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 main;
use strict;
use warnings;
use Benchmark ':all';
my $m = ClassMoose->new;
my $u = ClassMouse->new;
my $c = ClassCAF->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'};
},
}));
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.