Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.