tetsunosukeのnotebook

tetsunosukeのメモです

benchmarkしてみた。

ファイルの読みこみをする方法がいろいろあるみたいで、ちょっと気になってベンチマークをとってみました。

use Benchmark;

local $file = "bench.perl";
my $retry = 10000;

timethese(
    $retry,
    {
        test1 => '&func1()',
        test2 => '&func2()',
        test3 => '&func3()'
    }
);


sub func1()
{
    open(IN, $file);
    $array .= $_ while(<IN>);
    close IN;
    
    #print $array;
}

sub func2(){
    open(IN, $file);
    $array = do{local $/ = undef; <IN>};
    close IN;
    #print $array;
}

sub func3(){
    open(IN, $file);
    @stat = stat($file);
    read(IN, $array, $stat[7]);
    close IN;
    #print $array;
}
E:\work\perl\mixi>perl bench.perl
Benchmark: timing 10000 iterations of test1, test2, test3...
     test1: 20 wallclock secs ( 8.84 usr +  8.42 sys = 17.27 CPU) @ 579.21/s (n=10000)
     test2:  6 wallclock secs ( 1.69 usr +  2.47 sys =  4.16 CPU) @ 2406.16/s (n=10000)
     test3: 12 wallclock secs ( 3.03 usr +  6.28 sys =  9.31 CPU) @ 1073.88/s (n=10000)