Practice of Programming

プログラム とか Linuxとかの話題

Storable v.s. Data::Dumper

Storableが速い。

Benchmark: timing 3000 iterations of code1, code2...
     code1:  0 wallclock secs ( 0.57 usr +  0.00 sys =  0.57 CPU) @ 5263.16/s (n=3000)
     code2:  2 wallclock secs ( 1.96 usr +  0.00 sys =  1.96 CPU) @ 1530.61/s (n=3000)

コードはこんなの。

use Benchmark;

use strict;
use Storable;
use Data::Dumper;

$Data::Dumper::Purity = 1;
$Data::Dumper::Terse  = 1;
$Data::Dumper::Indent = 0;
$Data::Dumper::Pad    = '';

my $data =
  {
   a => 1,
   b => 1,
   c => {
	 a => 1,
	 b => 1,
	 c => 1,
	},
   dd => [qw/a b c d/],
   ee => [qw/aa bb cc dd/],
  };

my $code1 = sub{
  my $freeze = Storable::freeze($data);
  my $thaw   = Storable::thaw($freeze);
};

my $code2 = sub{
  my $freeze = Data::Dumper::Dumper($data);
  my $thaw   = eval($freeze);
};

timethese(3000,{code1 => $code1,code2 => $code2});