ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/eg/bench
(Generate patch)

Comparing JSON-XS/eg/bench (file contents):
Revision 1.16 by root, Tue Aug 28 02:06:06 2007 UTC vs.
Revision 1.22 by root, Tue Jan 19 01:02:19 2010 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3# Usage: bench json-file 3# Usage: bench json-file
4 4
5# which modules to test (JSON usually excluded because its so slow) 5# which modules to test (JSON::PP usually excluded because its so slow)
6my %tst = ( 6my %tst = (
7# "JSON" => ['JSON::objToJson $perl' , 'JSON::jsonToObj $json'], 7# "JSON" => ['JSON::encode_json $perl' , 'JSON::decode_json $json'],
8# "JSON::PP" => ['$pp->encode ($perl)' , '$pp->decode ($json)'], 8 "JSON::PP" => ['$pp->encode ($perl)' , '$pp->decode ($json)'],
9 "JSON::DWIW" => ['$dwiw->to_json ($perl)' , '$dwiw->from_json ($json)'], 9 "JSON::DWIW/FJ" => ['$dwiw->to_json ($perl)' , '$dwiw->from_json ($json)'],
10 "JSON::DWIW/DS" => ['$dwiw->to_json ($perl)' , 'JSON::DWIW::deserialize $json'],
10# "JSON::PC" => ['$pc->convert ($perl)' , '$pc->parse ($json)'], 11# "JSON::PC" => ['$pc->convert ($perl)' , '$pc->parse ($json)'],
11 "JSON::Syck" => ['JSON::Syck::Dump $perl' , 'JSON::Syck::Load $json'], 12 "JSON::Syck" => ['JSON::Syck::Dump $perl' , 'JSON::Syck::Load $json'],
12 "JSON::XS" => ['to_json $perl' , 'from_json $json'], 13 "JSON::XS" => ['encode_json $perl' , 'decode_json $json'],
13 "JSON::XS/2" => ['$xs2->encode ($perl)' , '$xs2->decode ($json)'], 14 "JSON::XS/2" => ['$xs2->encode ($perl)' , '$xs2->decode ($json)'],
14 "JSON::XS/3" => ['$xs3->encode ($perl)' , '$xs3->decode ($json)'], 15 "JSON::XS/3" => ['$xs3->encode ($perl)' , '$xs3->decode ($json)'],
15 "Storable" => ['Storable::nfreeze $perl' , 'Storable::thaw $pst'], 16 "Storable" => ['Storable::nfreeze $perl' , 'Storable::thaw $pst'],
16); 17);
17 18
18use JSON (); 19use JSON ();
19use JSON::DWIW; 20use JSON::DWIW;
20use JSON::PC; 21use JSON::PC;
21use JSON::PP (); 22use JSON::PP ();
22use JSON::XS qw(to_json from_json); 23use JSON::XS qw(encode_json decode_json);
23use JSON::Syck; 24use JSON::Syck;
24use Storable (); 25use Storable ();
25 26
26use Time::HiRes; 27use Time::HiRes;
27use List::Util; 28use List::Util;
38 39
39local $/; 40local $/;
40$json = <>; 41$json = <>;
41 42
42# fix syck-brokenised stuff 43# fix syck-brokenised stuff
43$json = JSON::XS->new->ascii(1)->encode (JSON::Syck::Load $json); 44#$json = JSON::XS->new->ascii(1)->encode (JSON::Syck::Load $json);
44 45
45#srand 0; $json = JSON::XS->new->utf8(1)->ascii(0)->encode ([join "", map +(chr rand 255), 0..2047]); 46#srand 0; $json = JSON::XS->new->utf8(1)->ascii(0)->encode ([join "", map +(chr rand 255), 0..2047]);
46 47
47#if (1) { 48#if (1) {
48# use Storable; 49# use Storable;
55#} 56#}
56 57
57sub bench($) { 58sub bench($) {
58 my ($code) = @_; 59 my ($code) = @_;
59 60
61 my $pst = Storable::nfreeze JSON::XS::decode_json $json; # seperately decode as storable stringifies :/
60 my $perl = JSON::XS::from_json $json; 62 my $perl = JSON::XS::decode_json $json;
61 my $pst = Storable::nfreeze $perl;
62 63
63 my $count = 5; 64 my $count = 5;
64 my $times = 200; 65 my $times = 200;
65 66
66 my $cent = eval "sub { my \$t = Time::HiRes::time; " . (join ";", ($code) x $count) . "; Time::HiRes::time - \$t }"; 67 my $cent = eval "sub { my \$t = Time::HiRes::time; " . (join ";", ($code) x $count) . "; Time::HiRes::time - \$t }";
75 } 76 }
76 77
77 return $count / $min; 78 return $count / $min;
78} 79}
79 80
80printf "%-10s | %10s | %10s |\n", "module", "encode", "decode"; 81printf "%-13s | %10s | %10s |\n", "module", "encode", "decode";
81printf "-----------|------------|------------|\n"; 82printf "--------------|------------|------------|\n";
82for my $module (sort keys %tst) { 83for my $module (sort keys %tst) {
83 my $enc = bench $tst{$module}[0]; 84 my $enc = bench $tst{$module}[0];
84 my $dec = bench $tst{$module}[1]; 85 my $dec = bench $tst{$module}[1];
85 86
86 printf "%-10s | %10.3f | %10.3f |\n", $module, $enc, $dec; 87 printf "%-13s | %10.3f | %10.3f |\n", $module, $enc, $dec;
87} 88}
88printf "-----------+------------+------------+\n"; 89printf "--------------+------------+------------+\n";
89 90

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines