TEST 1: COMPLEX PARAMETER CHECKING

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.

The validators tested were:

Data::Validator (shown as D:V in results table)

Using the StrictSequenced trait.

Params::Validate (shown as P:V in results table)

validate_pos given the following spec:

   state $spec = [
      {  type      => ARRAYREF,
      },
      {  can       => ["print", "say"],
      },
      {  type      => SCALAR,
         regex     => qr{^\d+$},
         callbacks => {
            'less than 90' => sub { shift() < 90 },
         },
      },
   ];
Params::Check (shown as P:C in results table)

Given three coderefs to validate parameters.

Type::Params::validate() (shown as T:P v in results table)

Called as:

   validate(\@_, ArrayRef, $PrintAndSay, $SmallInt);

Where $PrintAndSay is a duck type, and $SmallInt is a subtype of Int, with inlining defined.

Type::Params::compile() (shown as T:P c in results table)

Using the same type constraints as validate()

Results

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.)

TEST B: SIMPLE PARAMETER CHECKING

Based on the idea that I was playing to Type::Params' strengths, I decided to test a much simpler function signature. Here we check a function 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.

Results

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%      --

DEPENDENCIES

To run this script, you will need:

Type::Tiny::XS, Data::Validator, Params::Check, Params::Validate.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

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.