Skip to content

Instantly share code, notes, and snippets.

@mattn
Created February 17, 2009 09:02
Show Gist options
  • Select an option

  • Save mattn/65653 to your computer and use it in GitHub Desktop.

Select an option

Save mattn/65653 to your computer and use it in GitHub Desktop.
benchmark of JSON::XS encoding with ascii.
use strict;
use warnings;
use JSON::XS;
use Encode;
use Encode::JavaScript::UCS;
use Benchmark;
my $data = { text => "Foo[\x{2028}]Bar" };
my $xs = JSON::XS->new;
my $xsa = $xs->ascii;
timethese(500000, {
'JSON::XS ascii encoding' => '&test1;',
'JSON::XS(ascii) encoding' => '&test2;',
'Encode::JavaScript::UCS encoding' => '&test3;',
});
sub test1 { $xs->ascii->encode($data) }
sub test2 { $xsa->encode($data) }
sub test3 { encode( 'JavaScript-UCS', $xs->encode($data) ) };
#Benchmark: timing 500000 iterations of Encode::JavaScript::UCS encoding, JSON::XS ascii encoding, JSON::XS(ascii) encoding...
#Encode::JavaScript::UCS encoding: 31 wallclock secs (30.98 usr + 0.02 sys = 31.00 CPU) @ 16129.55/s (n=500000)
#JSON::XS ascii encoding: 3 wallclock secs ( 2.76 usr + 0.00 sys = 2.76 CPU) @ 180831.83/s (n=500000)
#JSON::XS(ascii) encoding: 2 wallclock secs ( 2.16 usr + 0.00 sys = 2.16 CPU) @ 231803.43/s (n=500000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment