public
Last active

Benchmarking simple accessors, Moose vs Mouse vs Moo vs hand written vs a hash

  • Download Gist
gistfile1.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
#!/usr/bin/env perl
 
use strict;
use warnings;
 
use Carp;
 
BEGIN {
# uncomment to test pure Perl Mouse
# $ENV{MOUSE_PUREPERL} = 1;
}
 
# ...........hash...............
 
my $hash = {};
sub hash_nc {
$hash->{bar} = 32;
my $x = $hash->{bar};
}
 
 
# ...........hash with check...............
 
my $hash_check = {};
sub hash {
my $arg = 32;
croak "we take an integer" unless defined $arg and $arg =~ /^[+-]?\d+$/;
$hash_check->{bar} = $arg;
my $x = $hash_check->{bar};
}
 
 
# ...........by hand..............
{
package Foo::Manual::NoChecks;
sub new { bless {} => shift }
sub bar {
my $self = shift;
return $self->{bar} unless @_;
$self->{bar} = shift;
}
}
my $manual_nc = Foo::Manual::NoChecks->new;
sub manual_nc {
$manual_nc->bar(32);
my $x = $manual_nc->bar;
}
 
 
# ...........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;
}
 
 
#.............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............
{
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;
}
 
 
#.............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;
}
 
 
#........... Moo using Sub::Quote..............
{
package Foo::Moo::QS;
use Moo;
use Sub::Quote;
has bar => (is => 'rw', isa => quote_sub q{ $_[0] =~ /^[+-]?\d+$/ });
}
my $mooqs = Foo::Moo::QS->new;
sub mooqs {
$mooqs->bar(32);
my $x = $mooqs->bar;
}
 
 
#............Object::Tiny..............
{
package Foo::Object::Tiny;
use Object::Tiny qw(bar);
}
my $ot = Foo::Object::Tiny->new( bar => 32 );
sub ot {
my $x = $ot->bar;
}
 
 
#............Object::Tiny::XS..............
{
package Foo::Object::Tiny::XS;
use Object::Tiny::XS qw(bar);
}
my $otxs = Foo::Object::Tiny::XS->new(bar => 32);
sub otxs {
my $x = $otxs->bar;
}
 
 
use Benchmark 'timethese';
 
print "Testing Perl $], Moose $Moose::VERSION, Mouse $Mouse::VERSION, Moo $Moo::VERSION\n";
timethese(
6_000_000,
{
# Moose => \&moose,
Mouse => \&mouse,
manual => \&manual,
"manual, no check" => \&manual_nc,
'hash, no check' => \&hash_nc,
hash => \&hash,
# Moo => \&moo,
# "Moo w/quote_sub" => \&mooqs,
"Object::Tiny" => \&ot,
"Object::Tiny::XS" => \&otxs,
}
);
 
 
__END__
Testing Perl 5.012002, Moose 1.24, Mouse 0.91, Moo 0.009007, Object::Tiny 1.08, Object::Tiny::XS 1.01
Benchmark: timing 6000000 iterations of Moo, Moo w/quote_sub, Moose, Mouse, Object::Tiny, Object::Tiny::XS, hash, manual, manual with no checks...
Object::Tiny::XS: 1 secs ( 1.20 usr + -0.01 sys = 1.19 CPU) @ 5042016.81/s
hash, no check : 3 secs ( 1.86 usr + 0.01 sys = 1.87 CPU) @ 3208556.15/s
Mouse : 3 secs ( 3.66 usr + 0.00 sys = 3.66 CPU) @ 1639344.26/s
Object::Tiny : 3 secs ( 3.80 usr + 0.00 sys = 3.80 CPU) @ 1578947.37/s
hash : 5 secs ( 5.53 usr + 0.01 sys = 5.54 CPU) @ 1083032.49/s
manual, no check: 9 secs ( 9.11 usr + 0.02 sys = 9.13 CPU) @ 657174.15/s
Moo : 17 secs (17.37 usr + 0.03 sys = 17.40 CPU) @ 344827.59/s
manual : 17 secs (17.89 usr + 0.02 sys = 17.91 CPU) @ 335008.38/s
Mouse no XS : 20 secs (20.50 usr + 0.03 sys = 20.53 CPU) @ 292255.24/s
Moose : 21 secs (21.33 usr + 0.03 sys = 21.36 CPU) @ 280898.88/s
Moo w/quote_sub : 23 secs (23.07 usr + 0.04 sys = 23.11 CPU) @ 259627.87/s

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.