Skip to content

Instantly share code, notes, and snippets.

@mashiro
Created August 12, 2013 10:17
Show Gist options
  • Select an option

  • Save mashiro/6209719 to your computer and use it in GitHub Desktop.

Select an option

Save mashiro/6209719 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
require 'benchmark'
require 'multi_json'
require 'json'
require 'yajl'
require 'oj'
N = 1000
TEST = [1, 100, 10000]
ENGINES = [:json_gem, :yajl, :oj]
Benchmark.bmbm do |bm|
el = ENGINES.map(&:to_s).map(&:length).max
tl = TEST.map(&:to_s).map(&:length).max
TEST.each do |size|
ENGINES.each do |t|
bm.report "#{t.to_s.rjust(el)} encode array #{size.to_s.rjust(tl)}" do
MultiJson.engine = t
data = (1..size).to_a
N.times { MultiJson.encode data }
end
end
end
TEST.each do |size|
ENGINES.each do |t|
bm.report "#{t.to_s.rjust(el)} encode object #{size.to_s.rjust(tl)}" do
MultiJson.engine = t
data = {}
size.times { |n| data[n.to_s] = n.to_s }
N.times { MultiJson.encode data }
end
end
end
TEST.each do |size|
ENGINES.each do |t|
bm.report "#{t.to_s.rjust(el)} decode array #{size.to_s.rjust(tl)}" do
MultiJson.engine = t
data = JSON.dump (1..size).to_a
N.times { MultiJson.decode data }
end
end
end
TEST.each do |size|
ENGINES.each do |t|
bm.report "#{t.to_s.rjust(el)} decode object #{size.to_s.rjust(tl)}" do
MultiJson.engine = t
data = {}
size.times { |n| data[n.to_s] = n.to_s }
data = JSON.dump data
N.times { MultiJson.decode data }
end
end
end
end
@mashiro
Copy link
Author

mashiro commented Aug 12, 2013

                                   user     system      total        real
json_gem encode  array     1   0.000000   0.000000   0.000000 (  0.006869)
    yajl encode  array     1   0.010000   0.000000   0.010000 (  0.009537)
      oj encode  array     1   0.010000   0.000000   0.010000 (  0.006519)
json_gem encode  array   100   0.010000   0.000000   0.010000 (  0.010244)
    yajl encode  array   100   0.030000   0.000000   0.030000 (  0.022408)
      oj encode  array   100   0.010000   0.000000   0.010000 (  0.007942)
json_gem encode  array 10000   0.400000   0.000000   0.400000 (  0.398003)
    yajl encode  array 10000   1.730000   0.010000   1.740000 (  1.735039)
      oj encode  array 10000   0.260000   0.000000   0.260000 (  0.264280)
json_gem encode object     1   0.010000   0.000000   0.010000 (  0.008073)
    yajl encode object     1   0.010000   0.000000   0.010000 (  0.008787)
      oj encode object     1   0.000000   0.000000   0.000000 (  0.006295)
json_gem encode object   100   0.090000   0.000000   0.090000 (  0.087198)
    yajl encode object   100   0.050000   0.000000   0.050000 (  0.044131)
      oj encode object   100   0.020000   0.000000   0.020000 (  0.014167)
json_gem encode object 10000   9.000000   0.110000   9.110000 (  9.123205)
    yajl encode object 10000   3.460000   0.180000   3.640000 (  3.646285)
      oj encode object 10000   1.190000   0.110000   1.300000 (  1.307293)
json_gem decode  array     1   0.010000   0.000000   0.010000 (  0.009266)
    yajl decode  array     1   0.010000   0.000000   0.010000 (  0.006817)
      oj decode  array     1   0.010000   0.000000   0.010000 (  0.006589)
json_gem decode  array   100   0.030000   0.000000   0.030000 (  0.026762)
    yajl decode  array   100   0.020000   0.000000   0.020000 (  0.021847)
      oj decode  array   100   0.020000   0.000000   0.020000 (  0.018154)
json_gem decode  array 10000   0.970000   0.000000   0.970000 (  0.977442)
    yajl decode  array 10000   1.330000   0.000000   1.330000 (  1.332911)
      oj decode  array 10000   0.510000   0.010000   0.520000 (  0.518458)
json_gem decode object     1   0.010000   0.000000   0.010000 (  0.009689)
    yajl decode object     1   0.000000   0.000000   0.000000 (  0.007538)
      oj decode object     1   0.010000   0.000000   0.010000 (  0.007134)
json_gem decode object   100   0.110000   0.000000   0.110000 (  0.116490)
    yajl decode object   100   0.090000   0.000000   0.090000 (  0.094101)
      oj decode object   100   0.050000   0.000000   0.050000 (  0.050388)
json_gem decode object 10000  13.010000   0.260000  13.270000 ( 13.271269)
    yajl decode object 10000   8.850000   0.020000   8.870000 (  8.880408)
      oj decode object 10000   6.120000   0.630000   6.750000 (  6.746470)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment