Of course, where this really matters is doing something not doing nothing and there, of course, the delta is less significant. (Though still greater than zero.)
package Foo;
sub do_something { my $i; $i++ for (1 .. 10) }
package main;
use strict;
use warnings;
use Benchmark qw( cmpthese );
my $foo = bless {}, 'Foo';
sub do_something { my $i; $i++ for (1 .. 10) }
cmpthese( -1, {
k1 => sub { $foo->do_something },
k2 => sub { Foo->do_something },
k3 => sub { Foo::do_something },
k4 => sub { do_something },
k5 => sub { &do_something },
});
Rate k2 k1 k4 k3 k5
k2 359975/s -- -3% -6% -7% -8%
k1 369628/s 3% -- -3% -5% -6%
k4 382293/s 6% 3% -- -2% -3%
k3 388120/s 8% 5% 2% -- -1%
k5 392991/s 9% 6% 3% 1% --
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.