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.5 by root, Sat Mar 24 22:10:09 2007 UTC vs.
Revision 1.9 by root, Tue Apr 10 03:47:26 2007 UTC

3# Usage: bench json-file 3# Usage: bench json-file
4 4
5use JSON; 5use JSON;
6use JSON::DWIW; 6use JSON::DWIW;
7use JSON::PC; 7use JSON::PC;
8use JSON::XS; 8use JSON::XS qw(to_json from_json);
9use JSON::Syck; 9use JSON::Syck;
10use Storable ();
10 11
11use Time::HiRes; 12use Time::HiRes;
12use List::Util; 13use List::Util;
13 14
14use utf8; 15use utf8;
15 16
16my $dwiw = new JSON::DWIW; 17my $dwiw = new JSON::DWIW;
17my $pc = new JSON::PC; 18my $pc = new JSON::PC;
18my $xs = JSON::XS->new->pretty->canonical->shrink; 19my $xs2 = JSON::XS->new->utf8->pretty->canonical;
20my $xs3 = JSON::XS->new->utf8->shrink;
19 21
20my $json; # the test string 22my $json; # the test string
21 23
22local $/; 24local $/;
23$json = <>; 25$json = <>;
24 26
27# fix syck-brokenised stuff
28$json = JSON::XS->new->ascii(1)->encode (JSON::Syck::Load $json);
29
25srand 0; $json = JSON::XS->new->utf8(1)->ascii(0)->encode ([join "", map +(chr rand 2555), 0..2047]); 30#srand 0; $json = JSON::XS->new->utf8(1)->ascii(0)->encode ([join "", map +(chr rand 255), 0..2047]);
26 31
27#if (1) { 32#if (1) {
28# use Storable; 33# use Storable;
29# open my $fh, "<:unix", "/opt/crossfire/share/cfserver/faces" or die "$!"; 34# open my $fh, "<:unix", "/opt/crossfire/share/cfserver/faces" or die "$!";
30# my $faces = Storable::thaw do { <$fh> }; 35# my $faces = Storable::thaw do { <$fh> };
34# warn length $json; 39# warn length $json;
35#} 40#}
36 41
37my %tst = ( 42my %tst = (
38# "JSON" => ['objToJson $perl' , 'jsonToObj $json'], 43# "JSON" => ['objToJson $perl' , 'jsonToObj $json'],
39# "JSON::DWIW" => ['$dwiw->to_json ($perl)', '$dwiw->from_json ($json)'], 44 "JSON::DWIW" => ['$dwiw->to_json ($perl)', '$dwiw->from_json ($json)'],
40# "JSON::PC" => ['$pc->convert ($perl)' , '$pc->parse ($json)'], 45 "JSON::PC" => ['$pc->convert ($perl)' , '$pc->parse ($json)'],
41# "JSON::Syck" => ['JSON::Syck::Dump $perl', 'JSON::Syck::Load $json'], 46 "JSON::Syck" => ['JSON::Syck::Dump $perl', 'JSON::Syck::Load $json'],
42 "JSON::XS" => ['to_json $perl' , 'from_json $json'], 47 "JSON::XS" => ['to_json $perl' , 'from_json $json'],
43 "JSON::XS/2" => ['$xs->encode ($perl)' , '$xs->decode ($json)'], 48 "JSON::XS/2" => ['$xs2->encode ($perl)' , '$xs2->decode ($json)'],
49 "JSON::XS/3" => ['$xs3->encode ($perl)' , '$xs3->decode ($json)'],
50 "Storable" => ['Storable::nfreeze $perl', 'Storable::thaw $pst'],
44); 51);
45 52
46sub bench($) { 53sub bench($) {
47 my ($code) = @_; 54 my ($code) = @_;
48 55
49 my $perl = jsonToObj $json; 56 my $perl = jsonToObj $json;
57 my $pst = Storable::nfreeze $perl;
58
50 my $count = 20; 59 my $count = 2;
51 my $times = 25; 60 my $times = 25;
52 61
53 my $cent = eval "sub { " . (join ";", ($code) x $count) . "}"; 62 my $cent = eval "sub { " . (join ";", ($code) x $count) . "}";
54 $cent->(); 63 $cent->();
55 64

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines