Skip to content

Instantly share code, notes, and snippets.

@fgabolde
Last active December 20, 2015 16:59
Show Gist options
  • Save fgabolde/6165823 to your computer and use it in GitHub Desktop.
Save fgabolde/6165823 to your computer and use it in GitHub Desktop.
Benchmarking Type::Tiny parameter validation, all three variants from the SYNOPSIS, plus Params::Validate thrown in for good measure.
$ perl bench-type-tiny.pl
Rate shortcutly params_validate stately lexically
shortcutly 9137/s -- -86% -96% -96%
params_validate 67202/s 635% -- -68% -68%
stately 207567/s 2172% 209% -- -2%
lexically 212347/s 2224% 216% 2% --
#!perl
use strict;
use warnings;
use 5.010;
use Carp;
use Params::Validate qw/validate_pos :types/;
use Type::Params qw/compile validate/;
use Types::Standard qw/slurpy Str ArrayRef Num/;
sub stately {
state $stateful_check = compile( Str, Str, slurpy ArrayRef[Num] );
my ($sort_code, $account_number, $monies) = $stateful_check->(@_);
return;
}
my $lexical_check;
sub lexically {
$lexical_check ||= compile( Str, Str, slurpy ArrayRef[Num] );
my ($sort_code, $account_number, $monies) = $lexical_check->(@_);
return;
}
sub shortcutly {
my ($sort_code, $account_number, $monies) =
validate( \@_, Str, Str, slurpy ArrayRef[Num] );
return;
}
sub params_validate {
my ($sort_code, $account_number, $monies) = validate_pos(@_, { type => SCALAR },
{ type => SCALAR },
(0) x (@_ - 2));
return;
}
use Benchmark qw/cmpthese/;
cmpthese(-10,
{ stately => sub { stately("12-34-56", "11223344", 1.2, 3, 99.99) },
lexically => sub { lexically("12-34-56", "11223344", 1.2, 3, 99.99) },
shortcutly => sub { shortcutly("12-34-56", "11223344", 1.2, 3, 99.99) },
params_validate => sub { params_validate("12-34-56", "11223344", 1.2, 3, 99.99) } });
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment