Skip to content

Instantly share code, notes, and snippets.

@tobyink
Created July 21, 2014 08:58
Show Gist options
  • Save tobyink/dcc15cf283c90c749501 to your computer and use it in GitHub Desktop.
Save tobyink/dcc15cf283c90c749501 to your computer and use it in GitHub Desktop.
use strictures;
use Types::Standard -types;
use Types::Common::Numeric -types;
use Types::Common::String -types;
use Benchmark qw(timethis);
{
my $check = PositiveInt;
my $data = 42;
my $sub = $check->compiled_check;
warn($check->inline_check(q/$i/));
$sub->(42) or die;
$sub->(0) and die;
$sub->(-42) and die;
$sub->([]) and die;
timethis 500_000, sub { $sub->($data) };
}
{
my $check = PositiveOrZeroInt;
my $data = 42;
my $sub = $check->compiled_check;
warn($check->inline_check(q/$i/));
$sub->(42) or die;
$sub->(0) or die;
$sub->(-42) and die;
$sub->([]) and die;
timethis 500_000, sub { $sub->($data) };
}
{
my $check = NonEmptyStr;
my $data = "Hello world";
my $sub = $check->compiled_check;
warn($check->inline_check(q/$str/));
$sub->("Hello world") or die;
$sub->("x") or die;
$sub->("") and die;
$sub->([]) and die;
timethis 500_000, sub { $sub->($data) };
}
__END__
PositiveInt...
initial speed: 210084.03/s
with xs speed: 1612903.23/s
PositiveOrZeroInt...
initial speed: 211864.41/s
with xs speed: 1515151.52/s
NonEmptyStr...
initial speed: 393700.79/s # the "Str" part of the check already uses XS
with xs speed: 1562500.00/s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment