Created
November 26, 2012 14:09
-
-
Save fgabolde/4148393 to your computer and use it in GitHub Desktop.
Comparing performance of common idioms for unpacking a subroutine's arguments
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 | |
use strict; | |
use warnings; | |
use 5.010; | |
use Carp; | |
use Params::Validate qw/validate/; | |
use Benchmark qw/cmpthese/; | |
sub with_shift { | |
my $self = shift; | |
my $key = shift; | |
my $value = shift; | |
} | |
sub with_list_lvalue { | |
my ($self, $key, $value) = @_; | |
} | |
sub with_validated_hash { | |
my $self = shift; | |
my %args = validate(@_, | |
{ foo => 1 }); | |
} | |
sub one_arg_shift { | |
my $self = shift; | |
} | |
sub one_arg_lvalue { | |
my ($self) = @_; | |
} | |
sub two_arg_shift { | |
my $self = shift; | |
my $other = shift; | |
} | |
sub two_arg_lvalue { | |
my ($self, $other) = @_; | |
} | |
my $self = {}; | |
# Params::Validate is several orders of magnitude slower than the | |
# rest so I ended up not including it in the bench | |
cmpthese(10000000, | |
{ 'one arg shift' => sub { one_arg_shift($self) }, | |
'one arg lvalue' => sub { one_arg_lvalue($self) } }); | |
cmpthese(10000000, | |
{ 'two arg shift' => sub { two_arg_shift($self, 18) }, | |
'two arg lvalue' => sub { two_arg_lvalue($self, 18) } }); | |
# shift starts losing here | |
cmpthese(10000000, | |
{ 'three arg shift' => sub { with_shift($self, foo => 'bar') }, | |
'three arg lvalue' => sub { with_list_lvalue($self, foo => 'bar') } }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment