Skip to content

Instantly share code, notes, and snippets.

@autarch
Last active June 22, 2017 16:39
Show Gist options
  • Select an option

  • Save autarch/be1aa29ee1cb5f762f84e3e459cc00fc to your computer and use it in GitHub Desktop.

Select an option

Save autarch/be1aa29ee1cb5f762f84e3e459cc00fc to your computer and use it in GitHub Desktop.
use v5.24;
{
package Mox;
use Moxie;
extends 'Moxie::Object';
has x => sub {0};
has y => sub {0};
sub x : ro;
sub y : ro;
}
{
package Moo;
use Moo;
has x => ( is => 'ro', default => 0 );
has y => ( is => 'ro', default => 0 );
}
{
package Moos;
use Moose;
has x => ( is => 'ro', default => 0 );
has y => ( is => 'ro', default => 0 );
}
{
package MoosI;
use Moose;
has x => ( is => 'ro', default => 0 );
has y => ( is => 'ro', default => 0 );
__PACKAGE__->meta->make_immutable;
}
use Benchmark qw( timethese );
timethese(
500_000,
{
'Moxie new' => sub { Mox->new },
'Moo new' => sub { Moo->new },
'Moose (not immutable) new' => sub { Moos->new },
'Moose (immutable) new' => sub { MoosI->new },
},
);
my $mox = Mox->new( x => 42 );
my $moo = Mox->new( x => 42 );
my $moos = Moos->new( x => 42 );
my $moosi = MoosI->new( x => 42 );
timethese(
10_000_000,
{
'Moxie accessor' => sub { $mox->x },
'Moo accessor' => sub { $moo->x },
'Moose (not immutable) accessor' => sub { $moos->x },
'Moose (immutable) accessor' => sub { $moosi->x },
},
);
__END__
$ perl ./foo
Benchmark: timing 500000 iterations of Moo new, Moose (immutable) new, Moose (not immutable) new, Moxie new...
Moo new: 1 wallclock secs ( 0.58 usr + 0.00 sys = 0.58 CPU) @ 862068.97/s (n=500000)
Moose (immutable) new: 1 wallclock secs ( 1.15 usr + 0.00 sys = 1.15 CPU) @ 434782.61/s (n=500000)
Moose (not immutable) new: 23 wallclock secs (23.01 usr + 0.00 sys = 23.01 CPU) @ 21729.68/s (n=500000)
Moxie new: 3 wallclock secs ( 2.71 usr + 0.00 sys = 2.71 CPU) @ 184501.85/s (n=500000)
# Note that Moxie was _slower_ in another run, so I think the differences are mostly noise
Benchmark: timing 10000000 iterations of Moo accessor, Moose (immutable) accessor, Moose (not immutable) accessor, Moxie accessor...
Moo accessor: 1 wallclock secs ( 1.38 usr + 0.00 sys = 1.38 CPU) @ 7246376.81/s (n=10000000)
Moose (immutable) accessor: 1 wallclock secs ( 1.31 usr + 0.00 sys = 1.31 CPU) @ 7633587.79/s (n=10000000)
Moose (not immutable) accessor: 1 wallclock secs ( 1.27 usr + 0.00 sys = 1.27 CPU) @ 7874015.75/s (n=10000000)
Moxie accessor: 1 wallclock secs ( 1.27 usr + 0.00 sys = 1.27 CPU) @ 7874015.75/s (n=10000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment