Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Benchmark: Storable, Sereal and JSON

by stevieb (Canon)
on Jun 30, 2021 at 15:38 UTC ( #11134502=perlmeditation: print w/replies, xml ) Need Help??

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!

Replies are listed 'Best First'.
Re: Benchmark: Storable, Sereal and JSON
by Your Mother (Archbishop) on Jun 30, 2021 at 17:08 UTC

    I have remarked frequently—well, at least two or three times—about how fast JSON::XS is. :P In combination with being human readable if pretty formatted, it’s really a good choice for any case it can meet.

Re: Benchmark: Storable, Sereal and JSON
by karlgoethebier (Abbot) on Jul 01, 2021 at 06:00 UTC

    Hi stevieb, probably you should also consider JSON::Tiny. Just an idea. Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Benchmark: Storable, Sereal and JSON
by Anonymous Monk on Jul 01, 2021 at 09:30 UTC

    And if _data returns an empty hash, then hapless pair gets blown beyond lunar orbit. The picture is different for even remotely complex/useful thing (such as). Besides, Sereal specifically, with dedicated pod document and bold emphasis, warns NOT to use stateless function interface if speed is important, use sereal_(en|de)code_with_object instead. This is not against JSON as good option, but in favour of better tests.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://11134502]
Approved by marto
Front-paged by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2021-09-19 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?