Benchmark: timing 1000000 iterations of blessed_hashref, hashref, mo, moose...
blessed_hashref: 1.80017 wallclock secs ( 1.79 usr + 0.00 sys = 1.79 CPU) @ 558659.22/s (n=1000000)
hashref: 1.37839 wallclock secs ( 1.38 usr + 0.00 sys = 1.38 CPU) @ 724637.68/s (n=1000000)
mo: 11.6823 wallclock secs (11.67 usr + 0.00 sys = 11.67 CPU) @ 85689.80/s (n=1000000)
moose: 14.8408 wallclock secs (14.82 usr + 0.00 sys = 14.82 CPU) @ 67476.38/s (n=1000000)
Rate moose mo blessed_hashref hashref
moose 67476/s -- -21% -88% -91%
mo 85690/s 27% -- -85% -88%
blessed_hashref 558659/s 728% 552% -- -23%
hashref 724638/s 974% 746% 30% --
#!/usr/bin/perl -w
package MooseState;
use Moose;
has 'name' => (
is => 'ro',
isa => 'Str',
required => 1,
);
has 'capital' => (
is => 'ro',
isa => 'Str',
required => 1,
);
has 'population' => (
is => 'rw',
isa => 'Int',
required => 1,
);
__PACKAGE__->meta->make_immutable();
package MoState;
use Mo qw(required);
has 'name' => (
is => 'ro',
isa => 'Str',
required => 1,
);
has 'capital' => (
is => 'ro',
isa => 'Str',
required => 1,
);
has 'population' => (
is => 'rw',
isa => 'Int',
required => 1,
);
package main;
use strict;
use warnings 'all';
use Benchmark qw( :all :hireswallclock );
my %args = (
name => 'Colorado',
capital => 'Denver',
population => 5_000_000,
);
my $results = timethese(1_000_000, {
blessed_hashref => \&blessed_hashref,
hashref => \&hashref,
moose => \&moose,
mo => \&mo,
});
cmpthese($results);
sub blessed_hashref
{
my $state = bless { %args }, 'Foo';
}# end blessed_hashref()
sub hashref
{
my $state = { %args };
}# end hashref()
sub moose
{
my $state = MooseState->new( %args );
}# end moose()
sub mo
{
my $state = MoState->new( %args );
}# end mo()
Is there something that can be done to make constructing Moose/Mo objects much, much faster?
I see now that this is a stacked comparison in that my benchmark script is having Moose do more work than it should (checking isa and required fields). As Your Mother pointed out, Mouse::XS may just be the ticket, as it's Moosey and still very fast.