use mop;
use Data::Dumper;
class mopClass {
has $foo is rw;
}
package ClassAccessorFastClass;
use strict;
use warnings;
use base 'Class::Accessor::Fast';
__PACKAGE__->mk_accessors('foo');
package main;
use Benchmark qw/timethese cmpthese/;
my $mop_obj = mopClass->new;
my $class_accessor_fast_obj = ClassAccessorFastClass->new;
my $hash = {};
my $result = timethese(100000, {
# mop accessor
mop_accessor => sub {
$mop_obj->foo('Hello');
my $foo = $mop_obj->foo;
},
# Class::Accessor::Fast accessor
accessor_fast_accessor => sub {
$class_accessor_fast_obj->foo('Hello');
my $foo = $class_accessor_fast_obj->foo;
},
# Direct hash
direct_hash => sub {
$hash->{foo} = 'Hello';
my $foo = $hash->{foo};
}
});
cmpthese $result;
Result.
Benchmark: timing 100000 iterations of accessor_fast_accessor, direct_hash, mop_accessor...
accessor_fast_accessor: 1 wallclock secs ( 0.17 usr + 0.00 sys = 0.17 CPU) @ 588235.29/s (n=100000)
(warning: too few iterations for a reliable count)
direct_hash: 0 wallclock secs ( 0.04 usr + 0.00 sys = 0.04 CPU) @ 2500000.00/s (n=100000)
(warning: too few iterations for a reliable count)
mop_accessor: 17 wallclock secs (17.22 usr + 0.06 sys = 17.28 CPU) @ 5787.04/s (n=100000)
Rate mop_accessor accessor_fast_accessor direct_hash
mop_accessor 5787/s -- -99% -100%
accessor_fast_accessor 588235/s 10065% -- -76%
direct_hash 2500000/s 43100% 325% --
Result.
mop accessor is very very slow.