初期化とか終了処理とか除いたベンチマークの取り方
ベンチマークを取る際に、それぞれの手法ごとに初期化や終了処理が必要なんだけど、それら処理を除いた、コアの部分の数値を比較したいことって結構ある。そういう場合は、以下のように書けばいい。ということを理解したのでメモ。
#! /usr/bin/perl use strict; use warnings; use Benchmark (); my %bench; Benchmark::enablecache(); for (my $i = 0; $i < 100; $i++) { # ここでいろいろ初期化 push_bench('a', sub { for (1..100000) {} }); # ここでいろいろ終了処理 } for (my $i = 0; $i < 100; $i++) { # ここでいろいろ初期化 push_bench('b', sub { for (1..200000) {} }); # ここでいろいろ終了処理 } Benchmark::cmpthese(\%bench); sub push_bench { my ($n, $c) = @_; my $t = Benchmark::timeit(1, $c); $bench{$n} = $bench{$n} ? Benchmark::timesum($bench{$n}, $t) : $t; }