Skip to content

Instantly share code, notes, and snippets.

@jeteve
Created December 11, 2015 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeteve/8e0365759d9c54c957c0 to your computer and use it in GitHub Desktop.
Save jeteve/8e0365759d9c54c957c0 to your computer and use it in GitHub Desktop.
Benchmarking Perl Moose against Moo
use strict;
use warnings;
use Benchmark;
{
package SomeBase;
sub method_a{
return 'fromBase';
}
1;
}
{
package SomeDelegate;
sub new{ return bless {},shift; }
sub delegated{
return 'From delegate';
}
1;
}
{
package MooRole;
use Moo::Role;
around 'method_b' => sub{
my ($orig, $self) = @_;
return 'Around '.$self->$orig();
};
1;
}
{
package WithMoo;
use Scalar::Util qw(looks_like_number);
use Moo;
extends qw/SomeBase/;
with qw/MooRole/;
has 'simple' => ( is => 'ro' , required => 1 );
has 'changeme' => ( is => 'rw' );
has 'anint' => ( is => 'ro', isa => sub{
die "$_[0] is not a number!" unless looks_like_number $_[0];
});
has 'changeint' => ( is => 'rw', isa => sub{
die "$_[0] is not a number!" unless looks_like_number $_[0];
});
has 'delegate' => ( is => 'rw',
isa => sub{ die "Not a SomeDelegate" unless $_[0]->isa('SomeDelegate');},
required => 1,
handles => ['delegated'],
);
around 'method_a' => sub{
my ($orig, $self) = @_;
return 'InMoo'.$self->$orig();
};
sub method_b{
my ($self) = @_;
return "I am ".$self;
}
1;
}
{
package MooseRole;
use Moose::Role;
around 'method_b' => sub{
my ($orig, $self) = @_;
return 'Around '.$self->$orig();
};
1;
}
{
package WithMoose;
use Moose;
extends qw/SomeBase/;
with qw/MooseRole/;
has 'simple' => ( is => 'ro' , required => 1 );
has 'changeme' => ( is => 'rw' );
has 'anint' => ( is => 'ro', isa => 'Int' );
has 'changeint' => ( is => 'rw', isa => 'Int' );
has 'delegate' => ( is => 'rw',
isa => 'SomeDelegate',
required => 1,
handles => ['delegated']);
around 'method_a' => sub{
my ($orig, $self) = @_;
return 'InMoose'.$self->$orig();
};
sub method_b{
my ($self) = @_;
return "I am ".$self;
}
__PACKAGE__->meta->make_immutable();
}
my $delegate = SomeDelegate->new();
print "Construction\n";
Benchmark::cmpthese(250_000,{
'moo' => sub{ my $o = WithMoo->new({ simple => 1 , anint => 1234 , delegate => $delegate }); },
'moose' => sub{ my $o => WithMoose->new({simple => 1 , anint => 1234 , delegate => $delegate }); }
});
my $moo = WithMoo->new({ simple => 1 , anint => 1234 , delegate => $delegate });
my $moose = WithMoose->new({ simple => 1 , anint => 1234 , delegate => $delegate });
print "\nSimple access\n";
{
my $i = 0;
Benchmark::cmpthese(2_000_000,{
'moo' => sub{ $i = $moo->simple(); },
'moose' => sub{ $i = $moose->simple(); }
});
}
print "\nTyped access\n";
{
my $i = 0;
Benchmark::cmpthese(2_000_000,{
'moo' => sub{ $i = $moo->anint(); },
'moose' => sub{ $i = $moose->anint(); }
});
}
print "\nObject access\n";
{
my $i = 0;
Benchmark::cmpthese(2_000_000,{
'moo' => sub{ $i = $moo->delegate(); },
'moose' => sub{ $i = $moose->delegate(); }
});
}
print "\nSimple setting\n";
{
my $i = 0;
Benchmark::cmpthese(2_000_000,{
'moo' => sub{ $moo->changeme( $i++ ); },
'moose' => sub{ $moose->changeme( $i++ ); }
});
}
print "\nBuilt-in Typed setting\n";
{
my $i = 0;
Benchmark::cmpthese(500_000,{
'moo' => sub{ $moo->changeint( $i++ ); },
'moose' => sub{ $moose->changeint( $i++ ); }
});
}
print "\nObject setting\n";
{
Benchmark::cmpthese(500_000,{
'moo' => sub{
my $o = SomeDelegate->new();
$moo->delegate($o);
},
'moose' => sub{
my $o = SomeDelegate->new();
$moose->delegate($o);
}
});
}
print "\nRole around method\n";
{
my $s = '';
Benchmark::cmpthese(500_000,{
'moo' => sub{ $s = $moo->method_b(); },
'moose' => sub{ $s = $moose->method_b(); }
});
}
print "\nAround method\n";
{
my $s = '';
Benchmark::cmpthese(500_000,{
'moo' => sub{ $s = $moo->method_a(); },
'moose' => sub{ $s = $moose->method_a(); }
});
}
print "\nDelegate method\n";
{
my $s = '';
Benchmark::cmpthese(500_000,{
'moo' => sub{ $s = $moo->delegated(); },
'moose' => sub{ $s = $moose->delegated(); }
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment