in reply to Re^3: Continuations in Perl - Returning to an arbitrary level up the call stack
in thread Continuations in Perl - Returning to an arbitrary level up the call stack

Your inerrant holiness who represents all of us!

> Indeed, whatever alternative mechanism is used, in whatever language, it will always be more costly than the natural return chain. The stack still has to be unwound

> Just as the exception mechanism in C++ (or structured exceptions in MSC), are a magnitude or more expensive because they have to search back up the stack looking for stack frames interpreting them as they go; so the goto that underlies such mechanisms in Perl has to go through an equally convoluted process to find a label at the appropriate scope.

Some heretics didn't fully trust you and tried to measure a time penalty for die or goto.

Look what they have done!

Timing for 1000000 recursions: Mode: return takes sec: 5.55880403518677 Mode: goto takes sec: 4.51015114784241 Mode: die takes sec: 1.70386385917664

use Time::HiRes qw/time/; no warnings 'recursion'; our $mode; sub rec { my $i=shift; $i--; if ($i<0) { return if $mode eq "return"; goto OUT if $mode eq "goto"; die "finished" if $mode eq "die"; } rec($i) } $|=1; my $recs=1000000; print "Timing for $recs recursions:\n" ; for $mode (qw/return goto die/) { print "\tMode: $mode takes sec: "; my $start=time; eval { rec($recs); }; OUT: # print $@ if $@; print time-$start,"\n"; }

Please prove them wrong!

May those heathens all burn in hell (...or emacs)!

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^5: Continuations in Perl - Returning to an arbitrary level up the call stack
by BrowserUk (Patriarch) on May 20, 2013 at 22:28 UTC

    He he. Thanks again. You get funnier and funnier.

    Let's see, what does this measure? Hm. Decrementing a variable a million times and doing nothing with the result.

    Okay then, this does that 30 times faster:

    #! perl -slw use strict; use Time::HiRes qw/time/; my $i = 1e6; my $start = time; 1 while --$i; printf "Took %f seconds\n", time - $start; __END__ C:\test>1034296.pl Took 0.062912 seconds

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Bielefeld does not exist!