Created
April 27, 2012 06:47
Benchmarking simple accessors, Moose vs Mouse vs Moo vs Object::InsideOut vs Method::Signatures vs hand-written
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
# ...........by hand with checks.............. | |
{ | |
package Foo::Manual; | |
use Carp; | |
sub new { bless {} => shift } | |
sub bar { | |
my $self = shift; | |
if (@_) { | |
# Simulate argument checking | |
my $arg = shift; | |
croak "we take an integer" unless defined $arg and $arg =~ /^[+-]?\d+$/; | |
$self->{bar} = $arg; | |
} | |
return $self->{bar}; | |
} | |
} | |
my $manual = Foo::Manual->new; | |
sub manual { | |
$manual->bar(32); | |
my $x = $manual->bar; | |
} | |
# ...........Method::Signatures .............. | |
{ | |
package Foo::Method::Signatures; | |
use Mouse (); | |
use Method::Signatures; | |
method new($class:) { | |
bless {}, $class; | |
} | |
method bar( Int $arg? ) { | |
return $self->{bar} if not $arg; | |
$self->{bar} = $arg; | |
} | |
} | |
my $msigs = Foo::Method::Signatures->new; | |
sub msigs { | |
$msigs->bar(32); | |
my $x = $msigs->bar; | |
} | |
#.............Object::InsideOut............. | |
{ | |
package Foo::Object::InsideOut; | |
use Object::InsideOut; | |
my @bar | |
: Field : Type(numeric) : Acc(bar); | |
} | |
my $oio = Foo::Object::InsideOut->new; | |
sub oio { | |
$oio->bar(32); | |
my $x = $oio->bar; | |
} | |
#.............Mouse............. | |
{ | |
package Foo::Mouse; | |
use Mouse; | |
has bar => ( is => 'rw', isa => 'Int' ); | |
__PACKAGE__->meta->make_immutable; | |
} | |
my $mouse = Foo::Mouse->new; | |
sub mouse { | |
$mouse->bar(32); | |
my $x = $mouse->bar; | |
} | |
#............Moose............ | |
=nomoose | |
{ | |
package Foo::Moose; | |
use Moose; | |
has bar => (is => 'rw', isa => "Int"); | |
__PACKAGE__->meta->make_immutable; | |
} | |
my $moose = Foo::Moose->new; | |
sub moose { | |
$moose->bar(32); | |
my $x = $moose->bar; | |
} | |
=cut | |
#.............Moo........... | |
{ | |
package Foo::Moo; | |
use Moo; | |
has bar => ( is => 'rw', isa => sub { $_[0] =~ /^[+-]?\d+$/ } ); | |
} | |
my $moo = Foo::Moo->new; | |
sub moo { | |
$moo->bar(32); | |
my $x = $moo->bar; | |
} | |
use Benchmark 'timethese'; | |
"Testing Perl $], Method::Signatures $Method::Signatures::VERSION, Moo $Moo::VERSION, Object::InsideOut $Object::InsideOut::VERSION, Mouse $Mouse::VERSION, Moose $Moose::VERSION\n"; | |
timethese( | |
6_000_000, | |
{ | |
'manual' => \&manual, | |
'Mouse' => \&mouse, | |
# 'Moose' => \&moose, | |
'Method::Signatures' => \&msigs, | |
'Object::InsideOut' => \&oio, | |
'Moo' => \&moo | |
} | |
); | |
__END__ | |
Testing Perl 5.014002, Method::Signatures 20111020, Moo 0.009011, Object::InsideOut 3.84, Mouse 0.97, Moose 2.0205 | |
Benchmark: timing 6000000 iterations of Method::Signatures, Moo, Mouse, Object::InsideOut, manual... | |
Method::Signatures: 17 wallclock secs (17.10 usr + 0.00 sys = 17.10 CPU) @ 350877.19/s (n=6000000) | |
Moo: 10 wallclock secs (10.42 usr + 0.00 sys = 10.42 CPU) @ 575815.74/s (n=6000000) | |
Mouse: 2 wallclock secs ( 1.97 usr + 0.00 sys = 1.97 CPU) @ 3045685.28/s (n=6000000) | |
Object::InsideOut: 5 wallclock secs ( 5.56 usr + 0.00 sys = 5.56 CPU) @ 1079136.69/s (n=6000000) | |
manual: 11 wallclock secs (10.55 usr + 0.00 sys = 10.55 CPU) @ 568720.38/s (n=6000000) | |
Moose: 12 wallclock secs (12.26 usr + 0.00 sys = 12.26 CPU) @ 489396.41/s (n=6000000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment