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