Skip to content

Instantly share code, notes, and snippets.

@makamaka
Created October 8, 2013 02:43
Show Gist options
  • Save makamaka/6878617 to your computer and use it in GitHub Desktop.
Save makamaka/6878617 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use Benchmark 'cmpthese';
cmpthese( -1, {
function => sub { my $hoge = 1; $hoge = negate($hoge) },
neg => sub { my $hoge = 1; $hoge = - $hoge },
minus => sub { my $hoge = 1; $hoge = 0 - $hoge },
multi => sub { my $hoge = 1; $hoge *= -1 },
bit => sub { use integer; my $hoge = 1; $hoge = 1 + ~$hoge },
} );
sub negate { - shift; }
__END__
Rate function neg bit minus multi
function 2548621/s -- -59% -64% -71% -74%
neg 6241523/s 145% -- -12% -28% -38%
bit 7057722/s 177% 13% -- -18% -29%
minus 8652585/s 240% 39% 23% -- -13%
multi 9986437/s 292% 60% 41% 15% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment