Skip to content

Instantly share code, notes, and snippets.

@schwern
Created January 10, 2011 06:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schwern/772455 to your computer and use it in GitHub Desktop.
Save schwern/772455 to your computer and use it in GitHub Desktop.
Benchmarking MooseX::Declare vs Method::Signatures vs hand written
#!/usr/bin/perl -w
use MooseX::Declare;
class WithDeclare {
has greetings =>
is => 'rw',
isa => 'Str',
default => "Hello"
;
has place =>
is => 'rw',
isa => 'Str',
default => "World"
;
method hello($greetings?, $place?) {
$greetings ||= $self->greetings;
$place ||= $self->place;
return "$greetings, $place!\n";
}
}
{
package WithoutDeclare;
use Method::Signatures;
use Moose;
has greetings =>
is => 'rw',
isa => 'Str',
default => "Hello"
;
has place =>
is => 'rw',
isa => 'Str',
default => "World"
;
method hello($greetings = $self->greetings, $place = $self->place) {
return "$greetings, $place!\n";
}
}
{
package PurePerl;
use strict;
use warnings;
sub new {
my $class = shift;
my %args = @_;
$args{greetings} ||= "Hello";
$args{place} ||= "World";
return bless \%args, $class;
}
sub greetings {
my $self = shift;
if( @_ ) {
$self->{greetings} = shift;
}
return $self->{greetings};
}
sub place {
my $self = shift;
if( @_ ) {
$self->{place} = shift;
}
return $self->{place};
}
sub hello {
my $self = shift;
my $greetings ||= $self->greetings;
my $place ||= $self->place;
return "$greetings, $place!\n";
}
}
use Benchmark;
my $mxd = WithDeclare->new;
my $ms = WithoutDeclare->new;
my $pp = PurePerl->new;
timethese shift || -3, {
mxdeclare => sub { $mxd->hello },
methodsig => sub { $ms->hello },
pureperl => sub { $pp->hello },
};
__END__
Benchmark: running methodsig, mxdeclare, pureperl for at least 3 CPU seconds...
methodsig: 4 wallclock secs ( 2.97 usr + 0.03 sys = 3.00 CPU) @ 344842.00/s (n=1034526)
mxdeclare: 4 wallclock secs ( 3.18 usr + 0.03 sys = 3.21 CPU) @ 8718.69/s (n=27987)
pureperl: 2 wallclock secs ( 3.08 usr + 0.02 sys = 3.10 CPU) @ 373916.77/s (n=1159142)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment