use strict;
use warnings;
use Benchmark qw(cmpthese);
use Time::Limit qw(20);
package MOOSE {
use Moose;
has a => (
reader => 'get_a',
writer => 'set_a',
isa => 'Int',
traits => ['Counter'],
handles => { adjust_a => 'inc' },
);
has b => (
reader => 'get_b',
writer => 'set_b',
isa => 'Int',
);
has c => (
reader => 'get_c',
writer => 'set_c',
isa => 'Int',
);
__PACKAGE__->meta->make_immutable;
}
package OO {
use Scalar::Util qw( looks_like_number );
sub new { bless $_[1], $_[0] }
sub get_a{ my( $o ) = @_; $o->{a} };
sub set_a{ my( $o, $v ) = @_; $o->{a} = $v };;
sub get_b{ my( $o ) = @_; $o->{b} };
sub set_b{ my( $o, $v ) = @_; $o->{b} = $v };;
sub get_c{ my( $o ) = @_; $o->{c} };
sub set_c{ my( $o, $v ) = @_; $o->{c} = $v };;
sub adjust_a{ my( $o ) = @_; $o->set_a( $o->get_a() + 1 ) };
}
my @AoH1 = map 'OO'->new({ a=>1, b=>2, c=>3 }), 1 .. 3e4;
my @AoH2 = map 'MOOSE'->new({ a=>1, b=>2, c=>3 }), 1 .. 3e4;
cmpthese -3, {
OO => sub { $_->adjust_a for @AoH1 },
MOOSE => sub { $_->adjust_a for @AoH2 },
};