Skip to content

Instantly share code, notes, and snippets.

@banyan
Created June 12, 2014 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banyan/f5b3e34200adeef57b5c to your computer and use it in GitHub Desktop.
Save banyan/f5b3e34200adeef57b5c to your computer and use it in GitHub Desktop.
oj benchmark
source 'https://rubygems.org'
gem 'multi_json'
gem 'yajl-ruby'
gem 'json'
gem 'oj'
# script comes from here: https://github.com/ohler55/oj/issues/78
require 'multi_json'
require 'benchmark'
require 'yajl'
require 'json'
require 'oj'
iter = 1_000_000
puts "Benchmarks for different JSON handlers with MultiJson."
puts " Ruby #{RUBY_VERSION}"
puts " #{iter} iterations"
MultiJson.engine = :oj
dt = Benchmark.realtime { iter.times { MultiJson.decode(%({"k1":"val1","k2":"val2","k3":"val3"})) }}
et = Benchmark.realtime { iter.times { MultiJson.encode(k1: "val1", k2: "val2", k3: "val3") }}
puts " Oj decode: #{dt} encode: #{et}"
MultiJson.engine = :yajl
dt = Benchmark.realtime { iter.times { MultiJson.decode(%({"k1":"val1","k2":"val2","k3":"val3"})) }}
et = Benchmark.realtime { iter.times { MultiJson.encode(k1: "val1", k2: "val2", k3: "val3") }}
puts " Yajl decode: #{dt} encode: #{et}"
MultiJson.engine = :json_gem
dt = Benchmark.realtime { iter.times { MultiJson.decode(%({"k1":"val1","k2":"val2","k3":"val3"})) }}
et = Benchmark.realtime { iter.times { MultiJson.encode(k1: "val1", k2: "val2", k3: "val3") }}
puts " Json decode: #{dt} encode: #{et}"
Oj.default_options = { :mode => :compat, :time_format => :ruby }
dt = Benchmark.realtime { iter.times { Oj.load(%({"k1":"val1","k2":"val2","k3":"val3"})) }}
et = Benchmark.realtime { iter.times { Oj.dump(k1: "val1", k2: "val2", k3: "val3") }}
puts "Raw Oj decode: #{dt} encode: #{et}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment