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.
| [reply] [d/l] [select] |
|
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.
| [reply] |
|
Thats good to know. I was just hoping that my program wouldn't suck up all the resources on my server. Thanks!
| [reply] |
|
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.
| [reply] |
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
| [reply] |
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
| [reply] |
|
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.
| [reply] |
|
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
| [reply] |
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.
| [reply] |