Skip to content

Instantly share code, notes, and snippets.

@xtetsuji
Created January 13, 2013 13:54
Show Gist options
  • Select an option

  • Save xtetsuji/4524216 to your computer and use it in GitHub Desktop.

Select an option

Save xtetsuji/4524216 to your computer and use it in GitHub Desktop.
Perl power operator (**) benchmark. Especially, compare 2-powered with same-values-times. see: https://twitter.com/tooooooooomy/status/290368608213794816 (en: In perl square caluration, same-values-times calcration is fast rather than 2-powered caluration. It is a small piece of knowledge.)
#!/usr/bin/perl
# xtetsuji 2013/01/13
use strict;
use warnings;
use Benchmark;
my $count = 50_000_000;
for my $x (-2, 0, 3, 5.5, 102, 7000) {
print ".................... \$x=$x ....................\n";
timethese($count, {
'same values times' => sub {
return $x * $x;
},
'2 powered' => sub {
return $x ** 2;
},
});
print "\n";
}
__END__
MEMO:
Benchmark results are varios it on my environment...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment