Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Speed comparison between Type::Params and Params::Validate (PP and XS)
use 5.012;
use Benchmark qw( cmpthese timethese );
use Test::Deep::NoTest;
use Scalar::Util qw(looks_like_number);
use Params::Validate qw(SCALAR HASHREF ARRAYREF);
my @SIG = (
{ type => SCALAR, callbacks => { looks_like_number => sub { looks_like_number($_[0]) } } },
{ type => HASHREF },
{ type => ARRAYREF },
);
{
package UsingParamsValidateXS;
use Params::Validate qw(validate_pos);
sub hash_plus {
my ($number, $hash, $keys) = validate_pos(@_, @SIG);
my %clone = %$hash;
$clone{$_} += $number for @$keys;
return \%clone;
}
}
{
package UsingParamsValidatePP;
use Params::Validate::PP;
sub hash_plus {
my ($number, $hash, $keys) = Params::Validate::PP::validate_pos(@_, @SIG);
my %clone = %$hash;
$clone{$_} += $number for @$keys;
return \%clone;
}
}
{
package UsingTypeParams;
use Type::Params qw(compile);
use Types::Standard qw( Num HashRef ArrayRef );
sub hash_plus {
state $check = compile(Num, HashRef, ArrayRef);
my ($number, $hash, $keys) = $check->(@_);
my %clone = %$hash;
$clone{$_} += $number for @$keys;
return \%clone;
}
}
{
package UsingSnail;
use Type::Params qw(compile);
use Types::Standard qw( Num HashRef ArrayRef );
sub hash_plus {
my $check = compile(Num, HashRef, ArrayRef);
my ($number, $hash, $keys) = $check->(@_);
my %clone = %$hash;
$clone{$_} += $number for @$keys;
return \%clone;
}
}
{
package UsingTypeParamsStricter;
use Type::Params qw(compile);
use Types::Standard qw( Num HashRef ArrayRef Str );
sub hash_plus {
state $check = compile(Num, HashRef[Num], ArrayRef[Str]);
my ($number, $hash, $keys) = $check->(@_);
my %clone = %$hash;
$clone{$_} += $number for @$keys;
return \%clone;
}
}
@::input = (7, { foo => 1, bar => 2, baz => 3 }, [qw/foo bar/]);
$::expected = { foo => 8, bar => 9, baz => 3 };
my %all;
for my $impl (qw/ UsingParamsValidateXS UsingParamsValidatePP UsingTypeParams /)
{
my $code = $impl->can("hash_plus");
die "bad implementation: $impl!\n" unless eq_deeply($code->(@::input), $::expected);
$all{$impl} = sprintf('%s::hash_plus(@::input)', $impl);
}
cmpthese(-3, \%all);
__END__
Rate UsingParamsValidatePP UsingParamsValidateXS UsingTypeParams
UsingParamsValidatePP 5071/s -- -64% -85%
UsingParamsValidateXS 14052/s 177% -- -59%
UsingTypeParams 34252/s 575% 144% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.