Skip to content

Instantly share code, notes, and snippets.

@tobyink
Created June 13, 2013 17:02
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 tobyink/5775423 to your computer and use it in GitHub Desktop.
Save tobyink/5775423 to your computer and use it in GitHub Desktop.
#!/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
Copy link

vpit commented Jun 13, 2013

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