Skip to content

Instantly share code, notes, and snippets.

@kfly8
Created December 8, 2017 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kfly8/baa26c59b0a0685427e08cdd92a55d72 to your computer and use it in GitHub Desktop.
Save kfly8/baa26c59b0a0685427e08cdd92a55d72 to your computer and use it in GitHub Desktop.
Benchmark Perl Type constraints
#!perl -w
use 5.10.0;
use strict;
use Benchmark qw(:all);
use Data::Validator;
use Function::Parameters qw/fun/;
use Kavorka fun => { -as => 'kavorka_fun' };
print "perl: $]\n";
print "modules:\n";
foreach my $mod (qw(Data::Validator Function::Parameters Kavorka)) {
print "\t", $mod, "/", $mod->VERSION, "\n";
}
sub dv_add {
state $v = Data::Validator->new(
x => { },
y => { },
);
my $args = $v->validate(@_);
return $args->{x} + $args->{y};
}
fun fp_add(:$x, :$y) {
return $x + $y;
}
kavorka_fun ka_add(:$x, :$y) {
return $x + $y;
}
print "without type constraints\n";
cmpthese -1, {
'D::Validator' => sub {
my $x = dv_add( x => 10, y => 10 );
},
'F::Parameters' => sub {
my $x = fp_add( x => 10, y => 10 );
},
'Kavorka' => sub {
my $x = ka_add( x => 10, y => 10 );
},
};
# perl: 5.026001
# validation modules:
# Data::Validator/1.07
# Function::Parameters/2.001003
# Kavorka/0.037
# without type constraints
# Rate Kavorka D::Validator F::Parameters
# Kavorka 332262/s -- -15% -72%
# D::Validator 390981/s 18% -- -67%
# F::Parameters 1183289/s 256% 203% --
#!perl -w
use 5.10.0;
use strict;
use Benchmark qw(:all);
use Types::Standard qw/Value/;
use Data::Validator;
use Function::Parameters qw/fun/;
use Kavorka fun => { -as => 'kavorka_fun' };
print "perl: $]\n";
print "modules:\n";
foreach my $mod (qw(Data::Validator Function::Parameters Kavorka)) {
print "\t", $mod, "/", $mod->VERSION, "\n";
}
print "type system: ";
print 'Types::Standard', '/', Types::Standard->VERSION, "\n";
sub dv_add {
state $v = Data::Validator->new(
x => Value,
y => Value,
);
my $args = $v->validate(@_);
return $args->{x} + $args->{y};
}
fun fp_add(Value :$x, Value :$y) {
return $x + $y;
}
kavorka_fun ka_add(Value :$x, Value :$y) {
return $x + $y;
}
print "with type constraints\n";
cmpthese -1, {
'D::Validator' => sub {
my $x = dv_add( x => 10, y => 10 );
},
'F::Parameters' => sub {
my $x = fp_add( x => 10, y => 10 );
},
'Kavorka' => sub {
my $x = ka_add( x => 10, y => 10 );
},
};
# perl: 5.026001
# validation modules:
# Data::Validator/1.07
# Function::Parameters/2.001003
# Kavorka/0.037
# type system: Types::Standard/1.002001
# with type constraints
# Rate D::Validator Kavorka F::Parameters
# D::Validator 172031/s -- -20% -62%
# Kavorka 214369/s 25% -- -53%
# F::Parameters 455903/s 165% 113% --
@kfly8
Copy link
Author

kfly8 commented Dec 8, 2017

compared Params::Validate, Smart::Args and Data::Validator
https://github.com/gfx/p5-Data-Validator/blob/master/benchmark/with-type.pl

Params::Validate/1.29
Smart::Args/0.14
Data::Validator/1.07
without type constraints
                    Rate       S::Args  P::Validate P::Validate/off D::Validator
S::Args         108687/s            --          -5%            -41%         -46%
P::Validate     114841/s            6%           --            -38%         -43%
P::Validate/off 184357/s           70%          61%              --          -8%
D::Validator    200971/s           85%          75%              9%           --

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment