public

Benchmarks for Class::Method::Modifiers and Class::Method::Modifiers::Fast

  • Download Gist
method-modifiers-benchmarking.t
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
use v5.14;
use Benchmark ':all';
use Test::More;
 
package Base {
no thanks;
sub new { bless \@_, shift }
sub foo { 1 };
sub bench {
my $class = shift;
for my $x (0 .. 100) {
my $obj = $class->new($x);
$obj->foo for 0 .. 10;
}
};
}
 
package CMM {
use base qw(Base);
use Class::Method::Modifiers;
around foo => sub { my $orig = shift; my $self = shift; $self->$orig(@_) + 1 };
}
 
package CMMF {
use base qw(Base);
use Class::Method::Modifiers::Fast;
around foo => sub { my $orig = shift; my $self = shift; $self->$orig(@_) + 1 };
}
 
diag sprintf("Testing Perl %s", $]);
diag sprintf("Testing CMM %s", 'Class::Method::Modifiers'->VERSION);
diag sprintf("Testing CMMF %s", 'Class::Method::Modifiers::Fast'->VERSION);
 
for my $class (qw/ CMM CMMF /)
{
is($class->new->foo, 2, "$class works");
my $bench = timethis(-3, sub { $class->bench }, undef, 'none');
diag sprintf("%s iterations: %d", $_, $bench->[5]);
}
 
done_testing();
 
__END__
# Testing Perl 5.016000
# Testing CMM 1.12
# Testing CMMF 0.041
ok 1 - CMM works
# iterations: 305
ok 2 - CMMF works
# iterations: 262
1..2

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.