PURPOSE
Compares the speed of the constructor in six equivalent classes built using different tools:
- Moo_MXTML
-
Moo with MooX::Types::MooseLike::Base types.
- Moo_TT
-
Moo with Type::Tiny types.
- Moose
- Moose_TT
-
Moose with Type::Tiny type constraints. Class is made immutable.
- Mouse
-
Mouse with Mouse type constraints. Class is made immutable. XS is switched off using
MOUSE_PUREPERL
environment variable. - Mouse_TT
-
Mouse with Type::Tiny type constraints. Class is made immutable. XS is switched off using
MOUSE_PUREPERL
environment variable.
Each tool is used to define a class like the following:
{
package Local::Class;
use Whatever::Tool;
use Types::Standard qw(HashRef ArrayRef Int);
has attr1 => (is => "ro", isa => ArrayRef[Int]);
has attr2 => (is => "ro", isa => HashRef[ArrayRef[Int]]);
}
Then we benchmark the following object instantiation:
Local::Class->new(
attr1 => [1..10],
attr2 => {
one => [0 .. 1],
two => [0 .. 2],
three => [0 .. 3],
},
);
RESULTS
In all cases, Type::Tiny type constraints are clearly faster than the conventional approach.
With Type::Tiny::XS:
Rate Moo_MXTML Mouse Moose Moo_TT Moose_TT Mouse_TT
Moo_MXTML 2428/s -- -35% -57% -82% -90% -91%
Mouse 3759/s 55% -- -33% -72% -85% -86%
Moose 5607/s 131% 49% -- -58% -78% -79%
Moo_TT 13274/s 447% 253% 137% -- -48% -51%
Moose_TT 25358/s 945% 575% 352% 91% -- -7%
Mouse_TT 27306/s 1025% 626% 387% 106% 8% --
Without Type::Tiny::XS:
Rate Moo_MXTML Mouse Moo_TT Moose Moose_TT Mouse_TT
Moo_MXTML 2610/s -- -31% -56% -56% -67% -67%
Mouse 3759/s 44% -- -36% -37% -52% -52%
Moo_TT 5894/s 126% 57% -- -1% -24% -25%
Moose 5925/s 127% 58% 1% -- -24% -25%
Moose_TT 7802/s 199% 108% 32% 32% -- -1%
Mouse_TT 7876/s 202% 110% 34% 33% 1% --
(Tested versions: Type::Tiny 0.045_03, Type::Tiny::XS 0.004, Moose 2.1207, Moo 1.005000, MooX::Types::MooseLike 0.25, and Mouse 2.3.0)
DEPENDENCIES
To run this script, you will need:
Type::Tiny::XS, Moo, MooX::Types::MooseLike::Base, Moose, Mouse.
AUTHOR
Toby Inkster <tobyink@cpan.org>.
COPYRIGHT AND LICENCE
This software is copyright (c) 2013-2014, 2017 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.