Perl でベンチマーク

Benchmark パッケージの timethese でベンチマークが取れる.

#!/usr/env/bin perl

use Benchmark;

$count = 1000;

timethese($count, {
    '$a = $a . "a"' => sub {
                           for ($i = 0; $i < 100; $i++) {
                               $a = $a . "a"
                           }
                       },
    '$a .= "a"    ' => sub {
                           for ($i = 0; $i < 100; $i++) {
                               $a .= "a"
                           }
                       },
});


出力

Benchmark: timing 10000 iterations of $a .= "a"    , $a = $a . "a"...
$a .= "a"    :  0 wallclock secs ( 0.17 usr +  0.00 sys =  0.17 CPU) @ 5882.35/s (n=1000)
$a = $a . "a": 85 wallclock secs (84.41 usr +  0.01 sys = 84.42 CPU) @ 11.85/s (n=1000)

・・・$a = $a . "a" はめたくそに遅い.

回数が少なすぎると

(warning: too few iterations for a reliable count)

と warning が出る