Skip to content

Instantly share code, notes, and snippets.

@mohnish
Created February 4, 2016 01:41
Show Gist options
  • Select an option

  • Save mohnish/662dc50c62bdd8c3c96a to your computer and use it in GitHub Desktop.

Select an option

Save mohnish/662dc50c62bdd8c3c96a to your computer and use it in GitHub Desktop.
# gem install json
# gem install oj
# gem install yajl-ruby
require 'benchmark'
require 'json'
require 'oj'
require 'yajl'
n = 100000
obj = [{"id"=>"0001", "type"=>"donut", "name"=>"Cake", "ppu"=>0.55, "batters"=>{"batter"=>[{"id"=>"1001", "type"=>"Regular"}, {"id"=>"1002", "type"=>"Chocolate"}, {"id"=>"1003", "type"=>"Blueberry"}, {"id"=>"1004", "type"=>"Devil's Food"}]}, "topping"=>[{"id"=>"5001", "type"=>"None"}, {"id"=>"5002", "type"=>"Glazed"}, {"id"=>"5005", "type"=>"Sugar"}, {"id"=>"5007", "type"=>"Powdered Sugar"}, {"id"=>"5006", "type"=>"Chocolate with Sprinkles"}, {"id"=>"5003", "type"=>"Chocolate"}, {"id"=>"5004", "type"=>"Maple"}]}, {"id"=>"0002", "type"=>"donut", "name"=>"Raised", "ppu"=>0.55, "batters"=>{"batter"=>[{"id"=>"1001", "type"=>"Regular"}]}, "topping"=>[{"id"=>"5001", "type"=>"None"}, {"id"=>"5002", "type"=>"Glazed"}, {"id"=>"5005", "type"=>"Sugar"}, {"id"=>"5003", "type"=>"Chocolate"}, {"id"=>"5004", "type"=>"Maple"}]}, {"id"=>"0003", "type"=>"donut", "name"=>"Old Fashioned", "ppu"=>0.55, "batters"=>{"batter"=>[{"id"=>"1001", "type"=>"Regular"}, {"id"=>"1002", "type"=>"Chocolate"}]}, "topping"=>[{"id"=>"5001", "type"=>"None"}, {"id"=>"5002", "type"=>"Glazed"}, {"id"=>"5003", "type"=>"Chocolate"}, {"id"=>"5004", "type"=>"Maple"}]}]
puts "=== dumping ==="
Benchmark.bm(7) do |x|
x.report("OJ") { n.times { Oj.dump(obj) } }
x.report("Yajl") { n.times { Yajl.dump(obj) } }
x.report("JS DUMP") { n.times { JSON.dump(obj) } }
x.report("JSON generate") { n.times { JSON.generate(obj) } }
x.report("#to_json") { n.times { obj.to_json } }
end
str = JSON.dump(obj)
puts "=== loading ==="
Benchmark.bm(7) do |x|
x.report("OJ") { n.times { Oj.load(str) } }
x.report("Yajl:") { n.times { Yajl.load(str) } }
x.report("JSON") { n.times { JSON.parse(str) } }
end
@mohnish

mohnish commented Feb 4, 2016

Copy link
Copy Markdown
Author
=== dumping ===
              user     system      total        real
OJ        0.850000   0.010000   0.860000 (  0.863370)
Yajl      1.970000   0.000000   1.970000 (  1.973006)
JS DUMP   4.000000   0.000000   4.000000 (  4.007164)
JSON generate  3.950000   0.000000   3.950000 (  3.950227)
#to_json  3.910000   0.000000   3.910000 (  3.905459)
=== loading ===
              user     system      total        real
OJ        3.120000   0.010000   3.130000 (  3.121486)
Yajl:     4.800000   0.000000   4.800000 (  4.811440)
JSON      5.570000   0.000000   5.570000 (  5.567986)

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