Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

subroutine calls itself

by tux402 (Acolyte)
on Jan 09, 2009 at 14:11 UTC ( [id://735186]=perlquestion: print w/replies, xml ) Need Help??

tux402 has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I have a question on coding practices. I have several subroutines within my script. None of them are nested, but many subroutines call themselves. Here is an example.
sub programControl { ... programControl(); }

Or I will have a subroutine which will go through another sub which will go back to itself. Such as,
sub programControl { ... subroutine1(); } sub subroutine1 { ... programControl(); }

I have some more complex sub calls, but those two are the basic idea. My program works, I'm just wondering about the performance issues. How does Perl handle this?

Replies are listed 'Best First'.
Re: subroutine calls itself
by ikegami (Patriarch) on Jan 09, 2009 at 14:21 UTC

    Your subroutines are said to be reentrant and (more specifically) recursive.

    How does Perl handle this?

    With ease.

    I'm just wondering about the performance issues.

    None. The first time a subroutine is called at a level of recursion it's never been before, there will be a tiny overhead as a new set of lexicals are created. These will be reused in future calls.

    my $c; my %s; sub foo { my ($x) = @_;; $c++; $s{\$x}++; foo($x-1) if $x; } for (1..1000) { foo(int(rand(6))+1); } print("foo() was called $c times, but only ", scalar(keys(%s)), " sets + of lexicals were created.\n");
    foo() was called 4531 times, but only 7 sets of lexicals were created.

      Well they're definitely recursive, but for his sake one hopes they're reentrant . . . :)

      (Reentrancy is a desirable property to aim for but it's not a given just because one recurses </nit>)

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

      Thats good to know. I was just hoping that my program wouldn't suck up all the resources on my server. Thanks!

        I just wanted to clarify something. When I said there's no performance issue, I meant compared to calling the same number of non-recursive subs. Calling subs in general is an expensive operation in Perl, but that's not related to whether the sub is recursive or not.

        So if you have no qualms with calling a sub seven times, you should have no qualms about calling a sub that recurses six times instead.

Re: subroutine calls itself
by swampyankee (Parson) on Jan 09, 2009 at 16:59 UTC

    As a philosophical point, don't worry about performance if it's adequate, unless you know that the recursive algorithm is incredibly far from optimal, like the using the most naive recursion to calculate Fibonacci numbers. Without the complete source, at least for the routines in question, nobody here could give a sensible answer to the question as to whether you should worry about potential performance problems, or not. Don't worry; as ikegami said, Perl handles recursion perfectly well.


    Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

Re: subroutine calls itself
by CountZero (Bishop) on Jan 09, 2009 at 14:56 UTC
    What kind of performance issue did you expect?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      I was just hoping that if the program is looping over and over that it won't suck up all my servers resources. As long as the program performance doesn't grow without bounds, I'm good.
        I was just hoping that if the program is looping over and over that it won't suck up all my servers resources.
        That is entirely up to the content of the sub-routine. If each call to your sub-routine fills a lexical array with a million floating point values and before you end the subroutine you call it again (and again, and again, ...) it will not take long before you exhaust your memory and then the swap-space and then your server dies. As such that has nothing to do with the fact that the subroutine calls itself, just that it was written badly.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: subroutine calls itself
by sundialsvc4 (Abbot) on Jan 09, 2009 at 16:27 UTC

    Recursive subroutines can produce large amounts of stack-consumption, but otherwise they are a well-accepted strategy for solving many kinds of problems. If the program that you have works acceptably well, given the volume and nature of the inputs that you have and the capacity of the machinery you have, “all is well.”

      Perl's stack resides on the heap, so stack consumption is a non-issue (unless XS code is being called recursively).

      There could be a difference in general memory consumption between a recursive and a non-recursive solution, but I wouldn't expect it to be major.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://735186]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-23 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found