public
Created

  • Download Gist
moo-vs-pop5.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
#!/usr/bin/env perl
 
use strict;
use warnings;
use Benchmark qw(cmpthese);
use Class::XSAccessor qw();
 
{
package Foo;
use Moo;
has id => (is => "ro", required => 1);
}
 
{
package Bar;
sub new {
my $class = shift;
my $self = bless {@_} => $class;
exists $self->{id} or die;
return $self;
}
sub id {
my $self = shift;
return $self->{id};
}
}
 
cmpthese(-1, {
UseMoo => q{ my $o = Foo->new(id => 42); $o->id for 1..100 },
UsePOP5 => q{ my $o = Bar->new(id => 42); $o->id for 1..100 },
});
 
__END__
Rate UsePOP5 UseMoo
UsePOP5 5023/s -- -54%
UseMoo 10882/s 117% --

Except that this benchmark compares a pure Perl accessor with an XS one, as Moo uses Class::XSAccessor to generate its getter, but that's not the case for your "plain old Perl 5" getter. Replace Bar::id by :

use Class::XSAccessor getters => { id => 'id' }

and see what happens.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.