Created
May 28, 2014 10:30
-
-
Save marramgg/0bb73ca0afd000a2875e to your computer and use it in GitHub Desktop.
Benchmark subroutine argument strategies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl | |
# source: http://www.perlmonks.org/?node_id=575918 | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results on my desktop: