Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: Why is goto &sub slow? by broquaint
in thread Why is goto &sub slow? by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found