"I seriously doubt my 3 y/o Core2Quad Q6600 @2.4GHz is twice as fast as your hardware."
I'm on a single core Intel Atom N270 clocked at 1.60 GHz.
"So the major difference probably comes down to the fact that I couldn't be bothered to install yet another slow-accessor constructor, so I manually code my Person Class"
Slow?! Ahem...
use v5.14;
use Benchmark qw(cmpthese);
package Foo1 {
sub new { bless $_[1], $_[0] }
sub foo { $_[0]{foo} }
}
package Foo2 {
use Moo;
has foo => (is => 'ro');
}
package Foo3 {
use Moose;
has foo => (is => 'ro');
}
# Note that I'm not even doing the make_immutable
# secret sauce with Moose because that only affects
# the speed of the constructor and destructor, not
# accessors.
my $foo1 = Foo1::->new({foo => 0});
my $foo2 = Foo2::->new({foo => 0});
my $foo3 = Foo3::->new({foo => 0});
cmpthese(1_000_000, {
plain_perl => sub { $foo1->foo },
moo => sub { $foo2->foo },
moose => sub { $foo3->foo },
});
Rate moose plain_perl moo
moose 304878/s -- -18% -73%
plain_perl 370370/s 21% -- -67%
moo 1136364/s 273% 207% --
Moo's accessors are stupidly fast because they use XS.
Even disabling XS via BEGIN { $ENV{MOO_XS_DISABLE} = 1 }, Moo's accessors are almost on par with the plain Perl version (about 7% slower).
As far as object construction goes, Moo is slower, but that's because Moo insists on some basic sanity checking as part of the constructor, which the plain Perl version shown above does not do.
But anyway, that could go some way to explaining:
"After that, why your results should be relatively different to mine I have no idea"
On my side I'm using Moo, so accessors are cheap calls. The functional implementation makes more calls to Person::name than your implementation, so slower accessors will cause it to drag.
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.