Compares the run-time speed of five parameter validators for validating a fairly complex function signature. The function accepts an arrayref, followed by an object providing print and say methods, followed by an integer less than 90.
print
say
The validators tested were:
Using the StrictSequenced trait.
StrictSequenced
validate_pos given the following spec:
validate_pos
state $spec = [ { type => ARRAYREF, }, { can => ["print", "say"], }, { type => SCALAR, regex => qr{^\d+$}, callbacks => { 'less than 90' => sub { shift() < 90 }, }, }, ];
Given three coderefs to validate parameters.
Called as:
validate(\@_, ArrayRef, $PrintAndSay, $SmallInt);
Where $PrintAndSay is a duck type, and $SmallInt is a subtype of Int, with inlining defined.
$PrintAndSay
$SmallInt
Int
Using the same type constraints as validate()
validate()
With Type::Tiny::XS:
Rate [D:V] [P:V] [P:C] [T:P v] [T:P c] [D:V] 10324/s -- -8% -35% -48% -81% [P:V] 11247/s 9% -- -29% -43% -80% [P:C] 15941/s 54% 42% -- -19% -71% [T:P v] 19685/s 91% 75% 23% -- -64% [T:P c] 55304/s 436% 392% 247% 181% --
Without Type::Tiny::XS:
Rate [P:V] [D:V] [P:C] [T:P v] [T:P c] [P:V] 9800/s -- -7% -8% -41% -72% [D:V] 10500/s 7% -- -1% -37% -71% [P:C] 10609/s 8% 1% -- -36% -70% [T:P v] 16638/s 70% 58% 57% -- -53% [T:P c] 35628/s 264% 239% 236% 114% --
(Tested versions: Data::Validator 1.04 with Mouse 2.3.0, Params::Validate 1.10, Params::Check 0.38, and Type::Params 0.045_03 with Type::Tiny::XS 0.004.)
Based on the idea that I was playing to Type::Params' strengths, I decided on something I thought would be more of a challenge: a simpler function signature which takes two required and one optional parameters. This is purely a test of parameter count; no type checking is involved!
This is a face off between Type::Params and Params::Validate.
Because no type checks are involved, it doesn't matter whether Type::Tiny::XS is available or not. (The results are similar either way.)
Rate [P:V] [T:P c] [P:V] 73643/s -- -70% [T:P c] 241917/s 228% --
To run this script, you will need:
Type::Tiny::XS, Data::Validator, Params::Check, Params::Validate.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2013-2014 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Type::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Type::Tiny
CPAN shell
perl -MCPAN -e shell install Type::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.