I revised the code slightly:

use Benchmark 'cmpthese'; sub get_value { my $self = shift; return $self->[0]; } sub get_value2 { $_[0]->[0]; } my $self = bless [123], '::main'; cmpthese( 16000000, { normal => sub { $self->get_value() }, optimized => sub { $self->get_value2() }, direct => sub { $self->[0] }, }); exit(0); $ perl oo_benchmark.pl Rate normal optimized direct normal 1462523/s -- -21% -90% optimized 1858304/s 27% -- -88% direct 15238095/s 942% 720% --

Yes, it took 16 million iterations to get an accurate reading on the direct attribute access. With a variant of your original benchmark modified to avoid the double-dispatch penalty, I get:

use Benchmark 'cmpthese'; my $self = bless [123], '::main'; cmpthese( 16000000, { normal => sub { $self->get_value() }, optimized => sub { $self->get_value2() }, direct => sub { $self->[0] }, }); exit(0); sub get_value { my $self = shift; return $self->[0]; } sub get_value2 { $_[0]->[0]; } $ perl oo_benchmark.pl Rate normal optimized direct normal 1725998/s -- -14% -94% optimized 2007528/s 16% -- -93% direct 29629630/s 1617% 1376% --

That represents speedups of around 20%, 11%, and 100%, respectively. I can make the case for a 10% speedup perhaps, but the others are way outside anything I can explain. Thus I'm not sure this benchmark is useful.


In reply to Re^3: Unexpected OO accessor benchmarks by chromatic
in thread Unexpected OO accessor benchmarks by cLive ;-)

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.