SPUG: Re: Loop styles: goto vs. redo
Jim Flanagan
jimfl at tensegrity.net
Tue Jul 2 14:23:29 CDT 2002
--On Tuesday, July 2, 2002 10:50 AM -0700 dancerboy
<dancerboy at strangelight.com> wrote:
> Why not? Because if you need to make several hundred iterations of
> Tests, you're likely to run out of memory and end up with a core
> dump.
>
> Remember: every time you make a subroutine call, the interpreter has
> to push a bunch of stuff onto the stack, and then pop it off the
> stack again at the end of the subroutine.
Except... this is a tail recursion (because nothing needs to happen
within the subroutine after the recursive call), and many language
compilers are smart enough to realize that this can be optimized not to
allocate a new stack frame for the call (a so-called "tail-call
elimination"). It basically becomes no less efficient than iteration.
I'm not sure if perl does this automagically, but you can get close
using, of all things, a goto (form 3):
Zub Tests {
my $done = 0;
for (...) {
unless (...) {
...
$done = 1;
}
}
$done and return;
goto &Tests;
}
--
Flanagan::Jim
http://jimfl.tensegrity.net
mailto:jimfl%40t%65ns%65gr%69ty.n%65t
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org
More information about the spug-list
mailing list