Skip to content

Instantly share code, notes, and snippets.

@schwern
Created April 1, 2011 00:26
Show Gist options
  • Save schwern/897540 to your computer and use it in GitHub Desktop.
Save schwern/897540 to your computer and use it in GitHub Desktop.
Benchmark quote_sub and Moo
use Foo;
use Benchmark qw(cmpthese);
my $obj = Foo->new;
cmpthese(shift, {
get_with_sub => sub {
$obj->with_sub(23) for 1..100;
},
get_with_qsub => sub {
$obj->with_qsub(23) for 1..100;
},
get_with_qsub_my => sub {
$obj->with_qsub_my(23) for 1..100;
}
});
cmpthese(shift, {
set_with_sub => sub {
my $x = $obj->with_sub(23) for 1..100;
},
set_with_qsub => sub {
my $x = $obj->with_qsub(23) for 1..100;
},
set_with_qsub_my => sub {
my $x = $obj->with_qsub_my(23) for 1..100;
},
});
package Foo;
use Moo;
use Sub::Quote;
has with_sub => (is => 'rw', isa => sub { $_[0] =~ /^[+-]?\d+$/ });
has with_qsub => (is => 'rw', isa => quote_sub q{ $_[0] =~ /^[+-]?\d+$/ });
has with_qsub_my => (is => 'rw', isa => quote_sub q{ my($thing) = @_; $thing =~ /^[+-]?\d+$/ });
1;
Rate get_with_qsub get_with_qsub_my get_with_sub
get_with_qsub 3664/s -- -8% -42%
get_with_qsub_my 3993/s 9% -- -37%
get_with_sub 6314/s 72% 58% --
Rate set_with_qsub set_with_qsub_my set_with_sub
set_with_qsub 3383/s -- -10% -24%
set_with_qsub_my 3757/s 11% -- -16%
set_with_sub 4467/s 32% 19% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment