SPUG: Re: Loop styles: goto vs. redo

dancerboy dancerboy at strangelight.com
Tue Jul 2 17:13:32 CDT 2002


At 12:23 pm -0700 2002-07-02, Jim Flanagan wrote:
>--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.

Even if the interpreter is "smart" enough not to allocate a new stack 
frame, the recursion is still going to be expensive:  the interpreter 
still needs to check the reference counts on all the variables that 
are local to the function, plus it's going to need to fudge the 
callstack somehow so that functions like caller() will return the 
expected results.

Having a smart compiler does not justify writing stupid code.

-jason

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     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