I am getting to a point in a project using OIO where I'll need to generate and query lots of short-lived objects, with speed being a concern. I have yet to code that aspect of the project and I have been wondering how OIO is going to fare in that kind of situation.
I was therefore very much interested in this use perl thread: http://use.perl.org/~hanekomu/journal/37775. I ran the same benchmark on my machine and added Object::InsideOut to the mix (see code below). I found that OIO performs at about the same level as Moose (the non-immutable version) unfortunately.
I was wondering if you had seen this particular use perl thread (from last November) and had any comments. I am particularly intrigued by the 'immutable' option of Moose, is the concept applicable to OIO?
Thanks
Jerome
#!/usr/bin/env perl
use warnings;
use strict;
use Benchmark qw(cmpthese timethese :hireswallclock);
package WithMoose;
use Moose;
has myattr => ( is => 'rw' );
package WithMooseImmutable;
use Moose;
has myattr => ( is => 'rw' );
__PACKAGE__->meta->make_immutable;
package WithObjectTiny;
use Object::Tiny qw/myattr/;
package WithObjectInsideOut;
use Object::InsideOut;{
my @myattr :Field :Get('myattr') :Arg('myattr');
}
package WithObjectInsideOutNoGen;
use Object::InsideOut;{
my @myattr :Field;
sub get_myattr {
my ($self) = @_;
return $myattr[$$self];
}
sub set_myattr {
my ($self,$value) = @_;
$myattr[$$self] = $value;
}
}
package main;
cmpthese(timethese(100_000,{
moose => sub {
my $obj = WithMoose->new(myattr => 27);
my $x = $obj->myattr;
},
moose_immutable => sub {
my $obj = WithMooseImmutable->new(myattr => 27);
my $x = $obj->myattr;
},
object_insideout => sub {
my $obj = WithObjectInsideOut->new(myattr => 27);
my $x = $obj->myattr;
},
object_insideout_no_gen => sub {
my $obj = WithObjectInsideOutNoGen->new;
$obj->set_myattr(27);
my $x = $obj->get_myattr;
},
object_tiny => sub {
my $obj = WithObjectTiny->new(myattr => 27);
my $x = $obj->myattr;
},
direct_hash => sub {
my $h = {};
$h->{myattr} = 27;
my $x = $h->{myattr};
},
}));
On my laptop I get this:
$ perl accessors_benchmark.pl
Benchmark: timing 100000 iterations of direct_hash, moose, moose_immutable, object_insideout, object_insideout_no_gen, object_tiny...
direct_hash: 0.434398 wallclock secs ( 0.44 usr + 0.00 sys = 0.44 CPU) @ 229357.80/s (n=100000)
moose: 33.1195 wallclock secs (33.06 usr + 0.00 sys = 33.06 CPU) @ 3025.17/s (n=100000)
moose_immutable: 2.38401 wallclock secs ( 2.39 usr + 0.00 sys = 2.39 CPU) @ 41893.59/s (n=100000)
object_insideout: 32.2816 wallclock secs (32.18 usr + 0.00 sys = 32.18 CPU) @ 3107.23/s (n=100000)
object_insideout_no_gen: 20.8383 wallclock secs (20.70 usr + 0.02 sys = 20.72 CPU) @ 4826.95/s (n=100000)
object_tiny: 1.02048 wallclock secs ( 0.97 usr + 0.00 sys = 0.97 CPU) @ 103412.62/s (n=100000)