Skip to content

Instantly share code, notes, and snippets.

@mix3
Created August 17, 2011 15:41
Show Gist options
  • Save mix3/1151814 to your computer and use it in GitHub Desktop.
Save mix3/1151814 to your computer and use it in GitHub Desktop.
fwbench.pl
package Class;
sub new {
my $class = shift;
my $self = shift || {};
return bless $self, $class;
}
sub add {
my $self = shift;
return $_[0] + $_[1];
}
1;
package main;
use Benchmark qw(cmpthese timethese);
sub add {
return $_[0] + $_[1];
}
my $instance = Class->new;
cmpthese timethese(100000, {
class => sub {
my $o = Class->new;
$o->add(1,1);
},
instance => sub {
$instance->add(1,1);
},
method => sub {
&add(1,1);
},
});
@mix3
Copy link
Author

mix3 commented Aug 17, 2011

$ perl fwbench.pl

Benchmark: timing 100000 iterations of class, instance, method...
     class:  0 wallclock secs ( 0.20 usr +  0.00 sys =  0.20 CPU) @ 500000.00/s (n=100000)
            (warning: too few iterations for a reliable count)
  instance:  0 wallclock secs ( 0.05 usr +  0.00 sys =  0.05 CPU) @ 2000000.00/s (n=100000)
            (warning: too few iterations for a reliable count)
    method:  0 wallclock secs ( 0.03 usr +  0.00 sys =  0.03 CPU) @ 3333333.33/s (n=100000)
            (warning: too few iterations for a reliable count)
              Rate    class instance   method
class     500000/s       --     -75%     -85%
instance 2000000/s     300%       --     -40%
method   3333333/s     567%      67%       --

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment