After I get a distribution very stable, I go back and try to make it better, faster or more efficient. In IPC::Shareable, I've been benchmarking various serialization techniques to see which one works the fastest. I knew that Sereal was quite a bit faster than Storable, but there are some gotchas with it that I couldn't work around. This morning I tested with JSON, and to my surprise, it blew both out of the water!
Results:
Benchmark: timing 5000000 iterations of json, sereal, store...
json: 17 wallclock secs (17.53 usr + 0.00 sys = 17.53 CPU) @ 28
+5225.33/s (n=5000000)
sereal: 22 wallclock secs (21.78 usr + 0.00 sys = 21.78 CPU) @ 22
+9568.41/s (n=5000000)
store: 49 wallclock secs (49.55 usr + 0.01 sys = 49.56 CPU) @ 10
+0887.81/s (n=5000000)
Rate store sereal json
store 102312/s -- -56% -64%
sereal 233863/s 129% -- -18%
json 286862/s 180% 23% --
Benchmark code:
use warnings;
use strict;
use Benchmark qw(:all) ;
use JSON qw(-convert_blessed_universally);
use Sereal qw(encode_sereal decode_sereal looks_like_sereal);
use Storable qw(freeze thaw);
if (@ARGV < 1){
print "\n Need test count argument...\n\n";
exit;
}
timethese($ARGV[0],
{
sereal => \&serial,
store => \&storable,
json => \&json,
},
);
cmpthese($ARGV[0],
{
sereal => \&serial,
store => \&storable,
json => \&json,
},
);
sub _data {
my %h = (
a => 1,
b => 2,
c => [qw(1 2 3)],
d => {z => 26, y => 25},
);
return \%h;
}
sub json {
my $data = _data();
my $json = encode_json $data;
my $perl = decode_json $json;
}
sub serial {
my $data = _data();
my $enc = encode_sereal($data);
my $dec = decode_sereal($enc);
}
sub storable {
my $data = _data();
my $ice = freeze($data);
my $water = thaw($ice);
}
I would never have expected that. Next up, a new serialization option for IPC::Shareable!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.