Created
June 13, 2013 17:02
-
-
Save tobyink/5775423 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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% -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :
and see what happens.