in reply to Unexpected OO accessor benchmarks

Am I missing something, or is that just plain weird?

Yes, you are. Try adding this to your script

use Data::Dumper; print Dumper { normal => $self->a(), optimized => $self->b(), direct => $self->c(), };
and you will see this:
$VAR1 = { 'direct' => 123, 'normal' => 123, 'optimized' => 123 };
Essentially you are benchmarking integers :)

-stvn

Replies are listed 'Best First'.
Re^2: Unexpected OO accessor benchmarks
by Fletch (Bishop) on Feb 09, 2007 at 20:10 UTC

    Yup, what he should have done is pass an anonymous coderef instead:

    normal => sub { $self->a() },

    Which gives reasonable results:

    Rate normal optimized direct normal 662252/s -- -15% -56% optimized 775194/s 17% -- -49% direct 1515152/s 129% 95% --

      Thank you - I feel much better now. Oh the fun of pre-processing...

      Rate normal optimized direct normal 172740/s -- -28% -60% optimized 239252/s 39% -- -45% direct 433898/s 151% 81% --

      That looks a little more like what I expected.

        It's not preprocessing. You pass a hashref to cmpthese, the values of which are computed when that line of code is executed. Those values in this case are method invocations which are evaluated where they occur. It should be no more surprising than some_sub( $obj->method() ) calling that sub with the value returned from the method call.