2009/06/07
Perlで書く 2009.06.07 Vol.235
===================================================================== Perlで書く 2009.06.07 Vol.235 [WAZA]ベンチマーク http://www2u.biglobe.ne.jp/~MAS/perl/ ===================================================================== ●ベンチマーク 処理時間を計測する場合にはBenchmarkモジュールが便利です。 次のサンプルで500となっているところは繰り返し実行する回数の指定です。 この部分に負数を指定すると、指定した数の秒数分CPUを消費するまで繰り 返すようになります。 --------------------------------------------------------------------- use strict; use warnings; use Benchmark qw(:all); timethese (500, { test_a => sub { my $a = 0; for (my $i = 0; $i < 10000; $i++) { $a++; } }, test_b => sub { my $a = 0; foreach (1 .. 10000) { $a++; } } } ); --------------------------------------------------------------------- 実行すると以下のような結果を出力します。 Benchmark: timing 500 iterations of test_a, test_b... test_a: 6 wallclock secs ( 2.24 usr + 0.00 sys = 2.24 CPU) @ 222.92/s (n=500) test_b: 4 wallclock secs ( 1.62 usr + 0.00 sys = 1.62 CPU) @ 308.26/s (n=500) timetheseの代わりにcmptheseを使うとそれぞれの実行結果の比較表を 出力します。 --------------------------------------------------------------------- use strict; use warnings; use Benchmark qw(:all); cmpthese (500, { test_a => sub { my $a = 0; for (my $i = 0; $i < 10000; $i++) { $a++; } }, test_b => sub { my $a = 0; foreach (1 .. 10000) { $a++; } } } ); --------------------------------------------------------------------- Rate test_a test_b test_a 217/s -- -19% test_b 267/s 23% -- 両方出したいときはtimetheseとcmptheseを両方書きます。 --------------------------------------------------------------------- use strict; use warnings; use Benchmark qw(:all); cmpthese timethese (500, { test_a => sub { my $a = 0; for (my $i = 0; $i < 10000; $i++) { $a++; } }, test_b => sub { my $a = 0; foreach (1 .. 10000) { $a++; } } } ); --------------------------------------------------------------------- Benchmark: timing 500 iterations of test_a, test_b... test_a: 6 wallclock secs ( 2.26 usr + 0.00 sys = 2.26 CPU) @ 220.95/s (n=500) test_b: 4 wallclock secs ( 2.05 usr + 0.00 sys = 2.05 CPU) @ 243.55/s (n=500) Rate test_a test_b test_a 221/s -- -9% test_b 244/s 10% -- --------------------------------------------------------------------- ●関連項目 ・times関数 http://www2u.biglobe.ne.jp/~MAS/perl/ref/times.html ・プロファイラ http://www2u.biglobe.ne.jp/~MAS/perl/waza/dprof.html ===================================================================== ■雑記 今回紹介したBenchmarkモジュールはすでにPerl表技集で紹介していた ものです。今回cmptheseについて追記したのでここでも紹介しました。 サンプルの実行結果は私のPCで実行した数値になります。皆さんのPCで 実行すると違った数字になると思いますので、実際に実行してみて数値を 比べてみるのも面白いと思います。私のPCは購入後6年以上経っているので これより遅い数字を出す人はほとんどいないのではないかと思います。 ===================================================================== Perlで書く(マガジンID:0000109251) 配信数:839 発行者:MAS 本メールマガジンに関するご意見・お問い合わせは t-masuda@mvd.biglobe.ne.jp までお寄せ下さい。 解除 http://www2u.biglobe.ne.jp/~MAS/perl/magazine.html 検索 http://www2u.biglobe.ne.jp/~MAS/perl/search/index.html バックナンバー http://archive.mag2.com/0000109251/index.html RSS http://archive.mag2.com/0000109251/rss10.xml 本メールマガジンは「まぐまぐ」 http://www.mag2.com/ を利用して発行 しています。 =====================================================================


