Create a gist now

Instantly share code, notes, and snippets.

#!/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% --
@vpit

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment