Skip to content

Instantly share code, notes, and snippets.

@mjsr
Created May 28, 2014 14:14
Show Gist options
  • Save mjsr/c8dfa232634e1d5636a9 to your computer and use it in GitHub Desktop.
Save mjsr/c8dfa232634e1d5636a9 to your computer and use it in GitHub Desktop.
Benchmark subroutine argument strategies v2
#!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(1) },
'use_list' => sub { sub_with_list(1) },
'use_direct' => sub { sub_with_direct(1) },
});
sub sub_with_shift
{
my $val = shift;
}
sub sub_with_list
{
my ($val) = @_;
}
sub sub_with_direct
{
my $val = $_[0];
}
@mjsr
Copy link
Author

mjsr commented May 28, 2014

Results:

$ perl benchmark_sub_args.v2.pl 
Benchmark: timing 1000000 iterations of use_direct, use_list, use_shift...
use_direct: -1 wallclock secs ( 0.10 usr +  0.00 sys =  0.10 CPU) @ 10000000.00/s (n=1000000)
            (warning: too few iterations for a reliable count)
  use_list:  0 wallclock secs ( 0.12 usr +  0.00 sys =  0.12 CPU) @ 8333333.33/s (n=1000000)
            (warning: too few iterations for a reliable count)
 use_shift:  0 wallclock secs ( 0.10 usr +  0.00 sys =  0.10 CPU) @ 10000000.00/s (n=1000000)
            (warning: too few iterations for a reliable count)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment