View difference between Paste ID: giBK030H and 1xhsTT5e
SHOW: | | - or go back to the newest paste.
1
require 'json'
2
require 'benchmark'
3
require 'oj'
4
require 'yajl'
5
 
6
a = {'asdfg'=> 'asdfg', 'qwert'=> 'asdfatqwtrqewt', 'rewqrq'=>543252, 'reqrqwer'=>[143245,1234421]}
7
j = a.to_json
8
9
N = (ARGV[0] || 100000).to_i
10
 
11
Benchmark.bmbm(6) do |x|
12-
#Benchmark.bm do |x|
12+
  x.report('json g') do N.times{a.to_json } end
13-
  x.report('json g') do N.times{|_| a['vczx'] = _; a.to_json} end
13+
  x.report('yajl g') do N.times{Yajl.dump(a)} end
14-
  x.report('yajl g') do N.times{|_| a['vczx'] = _; Yajl.dump(a)} end
14+
  x.report('oj   g') do N.times{Oj.dump(a)} end
15-
  x.report('oj   g') do N.times{|_| a['vczx'] = _; Oj.dump(a)} end
15+
  x.report('ojc  g') do N.times{Oj.dump(a, mode: :compat)} end
16-
  x.report('ojc  g') do N.times{|_| a['vczx'] = _; Oj.dump(a, mode: :compat)} end
16+
  x.report('json p') do N.times{JSON.parse(j)} end
17-
  x.report('json p') do N.times{|_| JSON.parse(j)} end
17+
  x.report('yajl p') do N.times{Yajl.load(j)} end
18-
  x.report('yajl p') do N.times{|_| Yajl.load(j)} end
18+
  x.report('oj   g') do N.times{Oj.load(j)} end
19-
  x.report('oj   g') do N.times{|_| Oj.load(j)} end
19+
  x.report('ojc  g') do N.times{Oj.load(j, mode: :compat)} end
20-
  x.report('ojc  g') do N.times{|_| Oj.load(j, mode: :compat)} end
20+
  # Correct use of Oj::Doc
21-
  x.report('ojdocg') do N.times{|_| Oj::Doc.parse(j).fetch} end
21+
  x.report('ojdocg') do N.times{Oj::Doc.open(j) { |x| x.fetch('asdfg') } } end
22-
end
22+
end
23
24
# results from ruby 1.9.3-p194 on OS X
25
26
              user     system      total        real
27
json g    0.700000   0.010000   0.710000 (  0.706307)
28
yajl g    0.660000   0.000000   0.660000 (  0.658921)
29
oj   g    0.490000   0.000000   0.490000 (  0.487269)
30
ojc  g    0.570000   0.000000   0.570000 (  0.571865)
31
json p    0.850000   0.000000   0.850000 (  0.851527)
32
yajl p    0.750000   0.000000   0.750000 (  0.744586)
33
oj   g    0.350000   0.000000   0.350000 (  0.351567)
34
ojc  g    0.450000   0.000000   0.450000 (  0.447477)
35
ojdocg    0.110000   0.000000   0.110000 (  0.114286)