Skip to content

Instantly share code, notes, and snippets.

@tobyink
Created November 2, 2012 14:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tobyink/4001692 to your computer and use it in GitHub Desktop.
Benchmarks for Class::Method::Modifiers and Class::Method::Modifiers::Fast
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment