Skip to content

Instantly share code, notes, and snippets.

@makamaka
Created August 18, 2010 07:19
Show Gist options
  • Save makamaka/533843 to your computer and use it in GitHub Desktop.
Save makamaka/533843 to your computer and use it in GitHub Desktop.
=pod
perl -I./lib benchmark/serialize.pl
-- serialize
JSON::PP: 2.27003
Data::MessagePack::PP: 0.01
Rate mp json
mp 158/s -- -15%
json 186/s 18% --
perl -I./lib benchmark/deserialize.pl
-- deserialize
JSON::PP: 2.27003
Data::MessagePack::PP: 0.01
Rate json mp
json 69.7/s -- -75%
mp 282/s 305% --
=cut
# serialize.pl
use strict;
use warnings;
my $is_pp = !$ARGV[0];
my $json = $is_pp ? 'JSON::PP' : 'JSON::XS';
my $msgp = $is_pp ? 'Data::MessagePack::PP' : 'Data::MessagePack';
eval qq{ require $json; $json->import; 1; } or die $@;
eval qq{ require $msgp } or die $@;
use Benchmark ':all';
my $se = $is_pp ? 2 ** 10 : 2 ** 24;
my $data = [
0..$se, { 'foo' => [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] },
'abcdefghijklmnopqrstuvwxyz',
];
print "-- serialize\n";
print "$json: ", $json->VERSION, "\n";
print "$msgp: ", $msgp->VERSION, "\n";
cmpthese(
-1 => {
json => sub { encode_json($data) },
mp => sub { $msgp->pack($data) },
}
);
# deserialize.pl
use strict;
use warnings;
my $is_pp = !$ARGV[0];
my $json = $is_pp ? 'JSON::PP' : 'JSON::XS';
my $msgp = $is_pp ? 'Data::MessagePack::PP' : 'Data::MessagePack';
eval qq{ require $json; $json->import; 1; } or die $@;
eval qq{ require $msgp } or die $@;
use Benchmark ':all';
my $se = $is_pp ? 2 ** 10 : 2 ** 24;
my $data = [ 0..$se, 'abcdefghijklmnopqrstuvwxyz' ];
# Data::MessagePack parses error.... why?
#$data = [
# 0..$se, { 'foo' => [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] },
# 'abcdefghijklmnopqrstuvwxyz',
#];
my $j = encode_json($data);
my $m = $msgp->pack($data);
print "-- deserialize\n";
print "$json: ", $json->VERSION, "\n";
print "$msgp: ", $msgp->VERSION, "\n";
cmpthese(
-1 => {
json => sub { decode_json($j) },
mp => sub { $msgp->unpack($m) },
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment