Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Why is goto &sub slow?

by broquaint (Abbot)
on Mar 29, 2004 at 01:30 UTC ( [id://340466]=note: print w/replies, xml ) Need Help??


in reply to Why is goto &sub slow?

When you perform a goto &sub you're not just calling the function, but you're also subverting the stack by destroying the current frame and any changes made by local within that frame. That's probably not quite the whole story, but I'd say that'd be the major cause of the discrepancy. Also you're tests aren't quite accurate in that the standard subroutine calls don't populate @_ whereas the goto &sub subroutines do. If you modify the tests you'll find the discrepancy will decrease1.
HTH

_________
broquaint

1 you'll find the test modified benchmark and results in the readmore below ...
use warnings; use strict; use Benchmark; use Test::More tests => 4; my $_fact3; $_fact3 = sub { my ($result, $counter, $max) = @_; return $result if $counter > $max; @_ = (($result * $counter), ($counter + 1), $max); goto $_fact3; }; my $number = shift or die "No number supplied"; validate_factorial_routines($number); print "Timing factorial of $number\n"; timethese(100_000, { 'recursion 1' => sub { fact0($number) }, 'recursion 2' => sub { fact1($number) }, 'typeglob' => sub { fact2($number) }, 'lexical' => sub { fact3($number) }, }); sub validate_factorial_routines { my $num = shift; my @result = (fact0($num), fact1($num), fact2($num), fact3($num)); is(fact0(5), 120, 'fact0 should return the correct amount'); is(fact1(5), 120, 'fact1 should return the correct amount'); is(fact2(5), 120, 'fact2 should return the correct amount'); is(fact3(5), 120, 'fact3 should return the correct amount'); } sub fact0 { my ($num) = @_; return _fact0(1,1,$num); } sub _fact0 { my ($result, $counter, $max) = @_; return $result if $counter > $max; return _fact1(($result * $counter), ($counter + 1), $max); } sub fact1 { @_ = ( 1, 1, $_[0] ); return &_fact1; } sub _fact1 { my ($result, $counter, $max) = @_; return $result if $counter > $max; @_ = (($result * $counter), ($counter + 1), $max); return &_fact1; } sub fact2 { my ($num) = @_; @_ = (1, 1, $num); goto &_fact2; } sub _fact2 { my ($result, $counter, $max) = @_; return $result if $counter > $max; @_ = (($result * $counter), ($counter + 1), $max); goto &_fact2; } sub fact3 { my ($num) = @_; @_ = (1, 1, $num); goto $_fact3; } __output__ 1..4 ok 1 - fact0 should return the correct amount ok 2 - fact1 should return the correct amount ok 3 - fact2 should return the correct amount ok 4 - fact3 should return the correct amount Timing factorial of 5 Benchmark: timing 100000 iterations of lexical, recursion 1, recursion + 2, typeglob... lexical: 17 wallclock secs (16.19 usr + 0.02 sys = 16.20 CPU) @ 61 +71.70/s (n=100000) recursion 1: 4 wallclock secs ( 3.74 usr + 0.00 sys = 3.74 CPU) @ 2 +6773.76/s (n=100000) recursion 2: 14 wallclock secs (13.41 usr + 0.00 sys = 13.41 CPU) @ 7 +459.35/s (n=100000) typeglob: 17 wallclock secs (17.61 usr + 0.00 sys = 17.61 CPU) @ 56 +78.91/s (n=100000)

update: fixed code type + updated results, thanks to Brutha for catching that

Replies are listed 'Best First'.
Re: Re: Why is goto &sub slow?
by Brutha (Friar) on Mar 29, 2004 at 07:43 UTC
    First, thanks Ovid and broquaint. This is a well done example of Perl, benchmarking and test in a different context than make test.

    broquaint, if I understand Your example, there is a typo in sub _fact0, it should return

    return _fact0(($result * $counter), ($counter + 1), $max);
    That makes recursion 1 twice as fast as recursion 2

    And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
    (Terry Pratchett, Small Gods)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 16:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found