Skip to content

Instantly share code, notes, and snippets.

@komarov
Created January 23, 2010 17:05
Rate method sub_validate sub
method 954/s -- -75% -100%
sub_validate 3842/s 303% -- -98%
sub 222222/s 23187% 5684% --
use MooseX::Declare;
class Foo {
use MooseX::Params::Validate;
method positional1( $bar, $baz = 42, $abcdef = 12 ) {
return $bar + $baz + $abcdef + int( rand( 10 ) );
}
sub positional2 {
my ($self,$bar,$baz,$abcdef) = @_;
# Default values
$baz = 42 if !defined $baz;
$abcdef = 12 if !defined $abcdef;
return $bar + $baz + $abcdef + int( rand( 10 ) );
}
sub positional3 {
my $self = shift;
my ( $bar, $baz, $abcdef ) = pos_validated_list(
\@_,
{ isa => 'Int' },
{ isa => 'Int', default => 42, optional => 1 },
{ isa => 'Int', default => 12, optional => 1 },
);
return $bar + $baz + $abcdef + int( rand( 10 ) );
}
}
package main;
use Benchmark qw( cmpthese );
my $Foo = Foo->new();
cmpthese(
100000,
{
'method' => sub { $Foo->positional1( 1 ) },
sub => sub { $Foo->positional2( 1 ) },
sub_validate => sub { $Foo->positional3( 1 ) },
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment