Skip to content

Instantly share code, notes, and snippets.

@aero aero/bench.pl
Created Jul 22, 2013

Embed
What would you like to do?
p5-mop-redux benchmark
#!/usr/bin/env perl
use 5.012;
use warnings;
use blib;
use Benchmark qw/cmpthese/;
use mop; # 2013-07-22
{
package Raw;
sub new {
return bless { a => 1 }, shift;
}
sub get_a {
my $self = shift;
return $self->{a};
}
sub set_a {
my $self = shift;
my $a = shift;
$self->{a} = $a;
}
}
class MOP {
has $a is rw = 1;
method get_a {
return $a;
}
method set_a($new_a) {
$a = $new_a;
}
}
cmpthese(100000, {
Raw_create => sub {
Raw->new();
},
MOP_create => sub {
MOP->new();
},
}
);
my $R = Raw->new();
my $M = MOP->new();
cmpthese(100000, {
Raw_get => sub {
$R->get_a;
},
MOP_get => sub {
$M->get_a;
},
}
);
cmpthese(100000, {
Raw_set => sub {
$R->set_a(3);
},
MOP_set => sub {
$M->set_a(3);
},
}
);
__END__
RESULT:
Rate MOP_create Raw_create
MOP_create 3127/s -- -100%
Raw_create 833333/s 26550% --
Rate MOP_get Raw_get
MOP_get 14749/s -- -100%
Raw_get 3333333/s 22500% --
Rate MOP_set Raw_set
MOP_set 13021/s -- -99%
Raw_set 2000000/s 15260% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.