On Feb 3, 2009, at 3:22 AM, Charles Oliver Nutter wrote:

> Do people generally use the native json gem for performance reasons?  
> Does a pure-ruby version not cut it? Are there benchmarks?

The documentation for the gems says:

# == Speed Comparisons
#
# I have created some benchmark results (see the benchmarks subdir of  
the
# package) for the JSON-Parser to estimate the speed up in the C  
extension:
#
# JSON::Pure::Parser::  28.90  calls/second
# JSON::Ext::Parser::  505.50 calls/second

The reason I like the extension is that it gets you pretty close to  
Marshal's speed:

#!/usr/bin/env ruby -wKU

require "benchmark"
require "yaml"

require "rubygems"
require "json"

TESTS = 10_000
DATA  = {"fields" => [1, 2.0, true, false, nil]}

Benchmark.bmbm do |results|
   results.report("JSON:")    { TESTS.times  
{ JSON.parse(DATA.to_json) } }
   results.report("Marshal:") { TESTS.times  
{ Marshal.load(Marshal.dump(DATA)) } }
   results.report("YAML:")    { TESTS.times  
{ YAML.load(DATA.to_yaml) } }
end
# >> Rehearsal --------------------------------------------
# >> JSON:      0.280000   0.030000   0.310000 (  0.306849)
# >> Marshal:   0.230000   0.040000   0.270000 (  0.267113)
# >> YAML:      5.090000   0.860000   5.950000 (  5.975268)
# >> ----------------------------------- total: 6.530000sec
# >>
# >>                user     system      total        real
# >> JSON:      0.270000   0.030000   0.300000 (  0.294798)
# >> Marshal:   0.230000   0.030000   0.260000 (  0.264460)
# >> YAML:      5.090000   0.870000   5.960000 (  5.965469)

With the pure version though, you are more like halfway between  
Marshal and YAML:

#!/usr/bin/env ruby -wKU

require "benchmark"
require "yaml"

require "rubygems"
require "json/pure"

TESTS = 10_000
DATA  = {"fields" => [1, 2.0, true, false, nil]}

Benchmark.bmbm do |results|
   results.report("JSON (pure):") { TESTS.times  
{ JSON.parse(DATA.to_json) } }
   results.report("Marshal:")     { TESTS.times  
{ Marshal.load(Marshal.dump(DATA)) } }
   results.report("YAML:")        { TESTS.times  
{ YAML.load(DATA.to_yaml) } }
end
# >> Rehearsal ------------------------------------------------
# >> JSON (pure):   2.780000   0.620000   3.400000 (  3.409480)
# >> Marshal:       0.230000   0.040000   0.270000 (  0.264554)
# >> YAML:          5.140000   0.880000   6.020000 (  6.040840)
# >> --------------------------------------- total: 9.690000sec
# >>
# >>                    user     system      total        real
# >> JSON (pure):   2.760000   0.630000   3.390000 (  3.407148)
# >> Marshal:       0.230000   0.040000   0.270000 (  0.264310)
# >> YAML:          5.130000   0.870000   6.000000 (  6.004295)

James Edward Gray II