Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/perl
package ClassMouse;
use Mouse;
has 'ro1' => ( is => 'ro', isa => 'Bool', default => 1 );
has 'ro2' => ( is => 'ro', isa => 'Int', default => 1 );
has 'ro3' => ( is => 'ro', isa => 'Str' , default => '1' );
has 'ro4' => ( is => 'ro', isa => 'ArrayRef' , default => sub { [] });
has 'ro5' => ( is => 'ro', isa => 'HashRef' , default => sub { {} });
has 'ro6' => ( is => 'ro', isa => 'CodeRef' , default => sub { sub {1} });
has 'rw1' => ( is => 'rw', isa => 'Bool', default => 1 );
has 'rw2' => ( is => 'rw', isa => 'Int', default => 1 );
has 'rw3' => ( is => 'rw', isa => 'Str' , default => '1' );
has 'rw4' => ( is => 'rw', isa => 'ArrayRef' , default => sub { [] });
has 'rw5' => ( is => 'rw', isa => 'HashRef' , default => sub { {} });
has 'rw6' => ( is => 'rw', isa => 'CodeRef' , default => sub { sub {1} });
no Mouse;
__PACKAGE__->meta->make_immutable;
package ClassMoose;
use Moose;
has 'ro1' => ( is => 'ro', isa => 'Bool', default => 1 );
has 'ro2' => ( is => 'ro', isa => 'Int', default => 1 );
has 'ro3' => ( is => 'ro', isa => 'Str' , default => '1' );
has 'ro4' => ( is => 'ro', isa => 'ArrayRef' , default => sub { [] });
has 'ro5' => ( is => 'ro', isa => 'HashRef' , default => sub { {} });
has 'ro6' => ( is => 'ro', isa => 'CodeRef' , default => sub { sub {1} });
has 'rw1' => ( is => 'rw', isa => 'Bool', default => 1 );
has 'rw2' => ( is => 'rw', isa => 'Int', default => 1 );
has 'rw3' => ( is => 'rw', isa => 'Str' , default => '1' );
has 'rw4' => ( is => 'rw', isa => 'ArrayRef' , default => sub { [] });
has 'rw5' => ( is => 'rw', isa => 'HashRef' , default => sub { {} });
has 'rw6' => ( is => 'rw', isa => 'CodeRef' , default => sub { sub {1} });
no Moose;
__PACKAGE__->meta->make_immutable;
package ClassCAF;
use strict;
use warnings;
use base 'Class::Accessor::Fast';
__PACKAGE__->mk_accessors(qw/rw1 rw2 rw3 rw4 rw5 rw6/);
package ClassCAL;
use strict;
use warnings;
use Class::Accessor::Lite (
new => 1,
rw => [ qw(rw1 rw2 rw3 rw4 rw5 rw6) ],
ro => [ qw(ro1 ro2 ro3 ro4 ro5 ro6) ],
);
package ClassBless;
use strict;
use warnings;
sub new {
my $class = shift;
bless +{ @_ }, $class;
}
package main;
use strict;
use warnings;
use Benchmark ':all';
my $m = ClassMoose->new;
my $u = ClassMouse->new;
my $c = ClassCAF->new;
my $l = ClassCAL->new;
no warnings 'void';
cmpthese(timethese(300000, {
moose_ro => sub {
$m->ro1;
$m->ro2;
$m->ro3;
$m->ro4;
$m->ro5;
$m->ro6;
},
moose_rw => sub {
$m->rw1;
$m->rw2;
$m->rw3;
$m->rw4;
$m->rw5;
$m->rw6;
},
moose_direct => sub {
$m->{'rw1'};
$m->{'rw2'};
$m->{'rw3'};
$m->{'rw4'};
$m->{'rw5'};
$m->{'rw6'};
},
mouse_ro => sub {
$u->ro1;
$u->ro2;
$u->ro3;
$u->ro4;
$u->ro5;
$u->ro6;
},
mouse_rw => sub {
$u->rw1;
$u->rw2;
$u->rw3;
$u->rw4;
$u->rw5;
$u->rw6;
},
mouse_direct => sub {
$u->{'rw1'};
$u->{'rw2'};
$u->{'rw3'};
$u->{'rw4'};
$u->{'rw5'};
$u->{'rw6'};
},
caf_rw => sub {
$c->rw1;
$c->rw2;
$c->rw3;
$c->rw4;
$c->rw5;
$c->rw6;
},
caf_direct => sub {
$c->{'rw1'};
$c->{'rw2'};
$c->{'rw3'};
$c->{'rw4'};
$c->{'rw5'};
$c->{'rw6'};
},
cal_rw => sub {
$l->rw1;
$l->rw2;
$l->rw3;
$l->rw4;
$l->rw5;
$l->rw6;
},
cal_ro => sub {
$l->ro1;
$l->ro2;
$l->ro3;
$l->ro4;
$l->ro5;
$l->ro6;
},
cal_direct => sub {
$l->{'rw1'};
$l->{'rw2'};
$l->{'rw3'};
$l->{'rw4'};
$l->{'rw5'};
$l->{'rw6'};
},
}));
cmpthese(timethese(300000, {
moose_new => sub {
ClassMoose->new;
},
mouse_new => sub {
ClassMouse->new;
},
caf_new => sub {
ClassCAF->new;
},
cal_new => sub {
ClassCAL->new;
},
bless_new => sub {
ClassBless->new;
},
}));
__END__
Rate moose_ro moose_rw cal_ro caf_rw cal_rw mouse_ro mouse_rw moose_direct mouse_direct caf_direct cal_direct
moose_ro 272727/s -- -0% -7% -19% -22% -58% -64% -82% -84% -85% -85%
moose_rw 272727/s 0% -- -7% -19% -22% -58% -64% -82% -84% -85% -85%
cal_ro 294118/s 8% 8% -- -13% -16% -55% -61% -80% -82% -84% -84%
caf_rw 337079/s 24% 24% 15% -- -3% -48% -55% -78% -80% -82% -82%
cal_rw 348837/s 28% 28% 19% 3% -- -47% -53% -77% -79% -81% -81%
mouse_ro 652174/s 139% 139% 122% 93% 87% -- -13% -57% -61% -65% -65%
mouse_rw 750000/s 175% 175% 155% 122% 115% 15% -- -50% -55% -60% -60%
moose_direct 1500000/s 450% 450% 410% 345% 330% 130% 100% -- -10% -20% -20%
mouse_direct 1666667/s 511% 511% 467% 394% 378% 156% 122% 11% -- -11% -11%
caf_direct 1875000/s 587% 587% 537% 456% 437% 187% 150% 25% 12% -- -0%
cal_direct 1875000/s 587% 587% 537% 456% 437% 187% 150% 25% 12% 0% --
Rate moose_new mouse_new caf_new cal_new bless_new
moose_new 21552/s -- -36% -96% -97% -98%
mouse_new 33520/s 56% -- -93% -96% -96%
caf_new 483871/s 2145% 1344% -- -44% -45%
cal_new 857143/s 3877% 2457% 77% -- -3%
bless_new 882353/s 3994% 2532% 82% 3% --
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.