雛形は以下。
$ cat bench.pl #!/usr/bin/perl -w use strict; use warnings; use Benchmark qw/cmpthese timethese/; my $count = 10000; my $obj2 = Hoge->new("hoge"); my $obj1 = Fuga->new("hoge"); cmpthese( timethese($count, {'TEST1' => '&test1;', 'TEST2' => '&test2;', }) ); exit; sub test1 { $obj1->fuga; } sub test2 { $obj2->hoge; } __END__
timetheseで経過時間、cmptheseをで速度比較結果を出力してくれる。
$ perl bench.pl Benchmark: timing 10000 iterations of TEST1, TEST2... TEST1: 0 wallclock secs ( 0.66 usr + 0.00 sys = 0.66 CPU) @ 15151.52/s (n=10000) TEST2: 33 wallclock secs (31.83 usr + 0.03 sys = 31.86 CPU) @ 313.87/s (n=10000) Rate TEST2 TEST1 TEST2 314/s -- -98% TEST1 15152/s 4727% --