Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: shift vs @_

by sgifford (Prior)
on Oct 02, 2006 at 22:14 UTC ( [id://575970]=note: print w/replies, xml ) Need Help??


in reply to Re^2: shift vs @_
in thread shift vs @_

They seem to be about the same. shift comes out slightly ahead in this test, perhaps because it simplifies the loop.
#!/usr/bin/perl use warnings; use strict; use Benchmark; timethese(1_000_000, { 'use_shift' => sub { sub_with_shift(0..9) }, 'use_list' => sub { sub_with_list(0..9) }, 'use_direct' => sub { sub_with_direct(0..9) }, }); sub sub_with_shift { my $sum = 0; while (@_) { $sum += shift; } $sum; } sub sub_with_list { my(@a)=@_; my $sum = 0; $sum += $_ for @a; $sum; } sub sub_with_direct { my $sum = 0; $sum += $_ for @_; $sum; }
Benchmark: timing 1000000 iterations of use_direct, use_list, use_shift...
use_direct:  7 wallclock secs ( 6.48 usr + -0.01 sys =  6.47 CPU) @ 154559.51/s (n=1000000)
  use_list: 10 wallclock secs ( 9.85 usr +  0.06 sys =  9.91 CPU) @ 100908.17/s (n=1000000)
 use_shift:  6 wallclock secs ( 6.48 usr +  0.01 sys =  6.49 CPU) @ 154083.20/s (n=1000000)

Replies are listed 'Best First'.
Re^4: shift vs @_
by Anonymous Monk on Oct 23, 2014 at 06:33 UTC
    Sorry i forgot result Benchmark: timing 1000000 iterations of use_direct, use_list, use_shif +t... use_direct: 0 wallclock secs ( 0.57 usr + 0.00 sys = 0.57 CPU) @ 17 +54385.96/s (n=1000000) use_list: 1 wallclock secs ( 0.86 usr + 0.00 sys = 0.86 CPU) @ 11 +62790.70/s (n=1000000) use_shift: 2 wallclock secs ( 0.96 usr + 0.00 sys = 0.96 CPU) @ 10 +41666.67/s (n=1000000)
Re^4: shift vs @_
by Anonymous Monk on Oct 23, 2014 at 06:31 UTC
    #!/usr/bin/perl use warnings; use strict; use Benchmark; timethese(1_000_000, { 'use_shift' => sub { sub_with_shift(0..2) }, 'use_list' => sub { sub_with_list(0..2) }, 'use_direct' => sub { sub_with_direct(0..2) }, }); sub sub_with_shift { my ($one, $two, $three) = (shift, shift, shift); my $sum = $one+$two+$three; } sub sub_with_list { my ($one, $two, $three) = @_; my $sum = $one+$two+$three; } sub sub_with_direct { my $sum = $_[0] + $_[1] + $_[2]; }
    i think my example better reflects the core of the problem

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-19 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found