1#!/usr/bin/perl 2 3use strict; 4use warnings; 5use Benchmark qw( cmpthese timethese ); 6 7our $VERSION = '1.00'; 8 9my $wanttime = $ARGV[1] || 5; 10 11use JSON qw( -support_by_pp -no_export ); # for JSON::PP::Boolean inheritance 12use JSON::PP (); 13use JSON::XS (); 14use utf8; 15 16my $pp = JSON::PP->new->utf8; 17my $xs = JSON::XS->new->utf8; 18 19local $/; 20 21my $json = <>; 22my $perl = JSON::XS::decode_json $json; 23my $result; 24 25 26printf( "JSON::PP %s\n", JSON::PP->VERSION ); 27printf( "JSON::XS %s\n", JSON::XS->VERSION ); 28 29 30print "-----------------------------------\n"; 31print "->encode()\n"; 32print "-----------------------------------\n"; 33 34$result = timethese( -$wanttime, 35 { 36 'JSON::PP' => sub { $pp->encode( $perl ) }, 37 'JSON::XS' => sub { $xs->encode( $perl ) }, 38 }, 39 'none' 40); 41cmpthese( $result ); 42 43print "-----------------------------------\n"; 44print "->pretty->canonical->encode()\n"; 45print "-----------------------------------\n"; 46 47$pp->pretty->canonical; 48$xs->pretty->canonical; 49 50$result = timethese( -$wanttime, 51 { 52 'JSON::PP' => sub { $pp->encode( $perl ) }, 53 'JSON::XS' => sub { $xs->encode( $perl ) }, 54 }, 55 'none' 56); 57cmpthese( $result ); 58 59print "-----------------------------------\n"; 60 61 62__END__ 63 64=pod 65 66=head1 SYNOPSYS 67 68 bench_encode.pl json-file 69 # or 70 bench_encode.pl json-file minimum-time 71 72=head1 DESCRIPTION 73 74L<JSON::PP> and L<JSON::XS> encoding benchmark. 75 76=head1 AUTHOR 77 78makamaka 79 80=head1 LISENCE 81 82This library is free software; you can redistribute it and/or modify it 83under the same terms as Perl itself. 84 85=cut 86 87