Cache::File と Cache::FileCache の比較

とある仕事でファイルキャッシュを使う機会があったので、似た名前、ほぼ同じ機能の Cache::File と Cache::FileCache をベンチマークしてみました。

導入障壁という意味でも、より依存するPerlモジュールの少ない Cache::FileCache に軍配が上がりました。

ベンチマーク結果

Benchmark: timing 50 iterations of Cache::File (write), Cache::FileCache (write)...
Cache::File (write): 19 wallclock secs ( 4.89 usr + 11.79 sys = 16.68 CPU) @  3.00/s (n=50)
Cache::FileCache (write):  4 wallclock secs ( 1.26 usr +  1.32 sys =  2.58 CPU) @ 19.38/s (n=50)
                           Rate     Cache::File (write) Cache::FileCache (write)
Cache::File (write)      3.00/s                      --                     -85%
Cache::FileCache (write) 19.4/s                    547%                       --
Benchmark: timing 50 iterations of Cache::File (read), Cache::FileCache (read)...
Cache::File (read): 16 wallclock secs ( 4.00 usr + 10.99 sys = 14.99 CPU) @  3.34/s (n=50)
Cache::FileCache (read):  1 wallclock secs ( 0.50 usr +  0.22 sys =  0.72 CPU) @ 69.44/s (n=50)
                          Rate      Cache::File (read) Cache::FileCache (read)
Cache::File (read)      3.34/s                      --                    -95%
Cache::FileCache (read) 69.4/s                   1982%                      --

スクリプト

use strict;
use warnings;
use Benchmark qw( timethese cmpthese );
use Cache::FileCache;
use Cache::File;

my $times = shift @ARGV || 50;
my @chars = ( 'a'..'z' );

my $filecache = Cache::FileCache->new( {
  namespace => 'bench',
  default_expires_in => 6000,
  cache_root => '/tmp',
} );
$filecache->Clear();

my $file = Cache::File->new(
  cache_root => '/tmp/bench',
  default_expires => '6000 sec',
);
$file->clear();


my $comp = timethese( $times, {
  'Cache::FileCache (write)' => sub {
    foreach( @chars ) {
      $filecache->set( $_, $_ x 32 );
    }
  },
  
  'Cache::File (write)' => sub {
    foreach( @chars ) {
      $file->set( $_, $_ x 32 );
    }
  },
} );
cmpthese( $comp );

my $comp2 = timethese( $times, {
  'Cache::FileCache (read)' => sub {
    foreach( @chars ) {
      my $data = $filecache->get( $_ );
    }
  },

  'Cache::File (read)' => sub {
    foreach( @chars ) {
      my $data = $file->get( $_ );
    }
  },
} );
cmpthese( $comp2 );

$filecache->Clear();
$file->clear();

1;
__END__