Skip to content

Instantly share code, notes, and snippets.

@jjn1056
Created August 11, 2010 13:55
Show Gist options
  • Save jjn1056/519011 to your computer and use it in GitHub Desktop.
Save jjn1056/519011 to your computer and use it in GitHub Desktop.
use Benchmark ':hireswallclock', qw(:all);
use MooseX::Types::Moose qw(Str Int ArrayRef HashRef);
use MooseX::Types::Structured qw(Tuple Dict);
use Moose::Util::TypeConstraints;
my $assigned_int;
my $assigned_array;
my $assigned_hash;
my $assigned_tuple;
my $assigned_dict;
my $subtype_int;
my $subtype_array;
my $subtype_hash;
my $subtype_tuple;
my $subtype_dict;
timethese( 10000, {
AssignTypes => sub {
$assigned_int = Int;
$assigned_array = ArrayRef[Int];
$assigned_hash = HashRef[Int];
$assigned_tuple = Tuple[Int];
$assigned_dict = Dict[first=>Int];
},
Subtypes => sub {
$subtype_int = subtype as Int;
$subtype_array = subtype as ArrayRef[Int];
$subtype_hash = subtype as HashRef[Int];
$subtype_tuple = subtype as Tuple[Int];
$subtype_dict = subtype as Dict[first=>Int];
},
});
timethese( 250000, {
IntAssigned => sub { $assigned_int->check(1) },
ArrayAssigned => sub { $assigned_array->check([1]) },
HashAssigned => sub { $assigned_hash->check({first=>1}) },
TupleAssigned => sub { $assigned_tuple->check([1]) },
DictAssigned => sub { $assigned_dict->check({first=>1}) },
IntSubtype => sub { $subtype_int->check(1) },
ArraySubtype => sub { $subtype_array->check([1]) },
HashSubtype => sub { $subtype_hash->check({first=>1}) },
TupleSubtype => sub { $subtype_tuple->check([1]) },
DictSubtype => sub { $subtype_dict->check({first=>1}) },
IntInline => sub { (Int)->check(1) },
ArrayInline => sub { (ArrayRef[Int])->check([1]) },
HashInline => sub { (HashRef[Int])->check({first=>1}) },
TupleInline => sub { (Tuple[Int])->check([1]) },
DictInline => sub { (Dict[first=>Int])->check({first=>1}) },
});
__END__
Benchmark: timing 10000 iterations of AssignTypes, Subtypes...
AssignTypes: 10.7071 wallclock secs (10.55 usr + 0.11 sys = 10.66 CPU) @ 938.09/s (n=10000)
Subtypes: 18.1497 wallclock secs (18.04 usr + 0.10 sys = 18.14 CPU) @ 551.27/s (n=10000)
Benchmark: timing 250000 iterations of ArrayAssigned, ArrayInline, ArraySubtype, DictAssigned, DictInline, DictSubtype, HashAssigned, HashInline, HashSubtype, IntAssigned, IntInline, IntSubtype, TupleAssigned, TupleInline, TupleSubtype...
ArrayAssigned: 3.61244 wallclock secs ( 3.62 usr + 0.00 sys = 3.62 CPU) @ 69060.77/s (n=250000)
ArrayInline: 54.6981 wallclock secs (54.59 usr + 0.03 sys = 54.62 CPU) @ 4577.08/s (n=250000)
ArraySubtype: 1.73294 wallclock secs ( 1.74 usr + 0.00 sys = 1.74 CPU) @ 143678.16/s (n=250000)
DictAssigned: 9.83754 wallclock secs ( 9.84 usr + 0.00 sys = 9.84 CPU) @ 25406.50/s (n=250000)
DictInline: 86.2538 wallclock secs (84.80 usr + 1.38 sys = 86.18 CPU) @ 2900.91/s (n=250000)
DictSubtype: 8.51632 wallclock secs ( 8.48 usr + 0.02 sys = 8.50 CPU) @ 29411.76/s (n=250000)
HashAssigned: 4.17924 wallclock secs ( 4.18 usr + 0.00 sys = 4.18 CPU) @ 59808.61/s (n=250000)
HashInline: 68.1211 wallclock secs (67.98 usr + 0.04 sys = 68.02 CPU) @ 3675.39/s (n=250000)
HashSubtype: 2.2522 wallclock secs ( 2.25 usr + 0.00 sys = 2.25 CPU) @ 111111.11/s (n=250000)
IntAssigned: 2.73726 wallclock secs ( 2.70 usr + 0.00 sys = 2.70 CPU) @ 92592.59/s (n=250000)
IntInline: 6.31222 wallclock secs ( 6.00 usr + 0.01 sys = 6.01 CPU) @ 41597.34/s (n=250000)
IntSubtype: 0.802971 wallclock secs ( 0.80 usr + 0.00 sys = 0.80 CPU) @ 312500.00/s (n=250000)
TupleAssigned: 8.40919 wallclock secs ( 8.41 usr + 0.01 sys = 8.42 CPU) @ 29691.21/s (n=250000)
TupleInline: 84.4781 wallclock secs (82.78 usr + 1.62 sys = 84.40 CPU) @ 2962.09/s (n=250000)
TupleSubtype: 6.95139 wallclock secs ( 6.92 usr + 0.00 sys = 6.92 CPU) @ 36127.17/s (n=250000)
John-Napiorkowski-MacBook-Pro:MooseX-Types-Structured johnn$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment