小池啓仁 ヒロヒト応援ブログ By はてな

小池啓仁(コイケヒロヒト)の動画など。

小池啓仁 ヒロヒト応援ブログ By はてな

Benchmark モジュールの使い方

いつも陰ながらお世話になっているid:fbisさんところで、各種コンスタントのBenchmarkテストがありました。

Benchmarkは使ったことがないと思ったのですが、ググルとなんと9年前の2000年の掲示板に自分の書き込みあり、benchmarkを使っているのです。

しかし、すっかり忘れている自分・・・ってPerlは5年以上ブランクがあるので・・・。
で、引きつづきググルと良いページを2つ見つけました!

最後にPerldoc Benchmarkで使い方のところを引用しときます。

NAME
    Benchmark - benchmark running times of Perl code

SYNOPSIS
        use Benchmark qw(:all) ;

        timethis ($count, "code");

        # Use Perl code in strings...
        timethese($count, {
            'Name1' => '...code1...',
            'Name2' => '...code2...',
        });

        # ... or use subroutine references.
        timethese($count, {
            'Name1' => sub { ...code1... },
            'Name2' => sub { ...code2... },
        });

        # cmpthese can be used both ways as well
        cmpthese($count, {
            'Name1' => '...code1...',
            'Name2' => '...code2...',
        });

        cmpthese($count, {
            'Name1' => sub { ...code1... },
            'Name2' => sub { ...code2... },
        });

        # ...or in two stages
        $results = timethese($count,
            {
                'Name1' => sub { ...code1... },
                'Name2' => sub { ...code2... },
            },
            'none'
        );
        cmpthese( $results ) ;