Created
January 13, 2013 13:54
-
-
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.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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