grepとzgrepの速度差

この前、周りで話題に出てたのでgrepとzgrepのベンチをとってみた。
サンプルはアクセスログでYahooという文字列をgrepする。ファイルは11Mくらい。

要点は圧縮しない状態だとどれくらい速いのかしら?という事。
容量を圧迫する事を犠牲にしてでも良いくらいのパフォーマンスの差がでるかどうか。

まず圧縮。

$ diff access_log.1 access_log.2
$ gzip access_log.2
$ ls -lha access_log.*
-rw-r--r--  1 yumatsumo  yumatsumo       11M Feb 13 00:32 access_log.1
-rw-r--r--  1 yumatsumo  yumatsumo        1M Feb 13 00:21 access_log.2.gz

そしてYahooの出現回数を数えるサブルーチンで比較。

use strict;
use warnings;

use Benchmark;
use FindBin qw($Bin);

use Fatal qw(open close);

sub count_yahoo($)  {
    my $fh = shift;
    my $counter = 0;
    while (my $row = <$fh>) {
        $counter++;
    }
}

sub comp {
    open my $fh, "grep Yahoo $Bin/access_log.1 |";
    count_yahoo $fh;
    close $fh;
}

sub uncomp {
    open my $fh, "zgrep Yahoo $Bin/access_log.2.gz |";
    count_yahoo $fh;
    close $fh;
}

my $b = Benchmark::timethese(1000, {
            comp   => \&comp,
            uncomp => \&uncomp,
          });

Benchmark::cmpthese($b);

結果はこんな感じ。圧縮してない方がやや速いくらい。

Benchmark: timing 1000 iterations of comp, uncomp...
      comp: 54 wallclock secs ( 2.30 usr 15.27 sys + 14.37 cusr 21.12 csys = 53.06 CPU) @ 56.92/s (n=1000)
    uncomp: 201 wallclock secs ( 0.57 usr 12.74 sys + 31.15 cusr 154.74 csys = 199.20 CPU) @ 75.13/s (n=1000)
         Rate   comp uncomp
comp   56.9/s     --   -24%
uncomp 75.1/s    32%     --

だけどギガ単位のファイルだとまた違って来るか。うーむ。
今度ファイルサイズ変えて段階的にとってみよう。