-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_parse.rb
76 lines (64 loc) · 1.95 KB
/
benchmark_parse.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'jsoncons'
require 'json'
require 'benchmark'
MILLION_OF_JSONS = 1_000_000.times.map { %Q`{"data":[#{rand}]}` }
MILLION_OF_ELEMENTS = %Q`{"data":[#{1_000_000.times.map { rand }.join(',')}]}`
Benchmark.bm(35) do |x|
x.report("JSON MILLION_OF_JSONS sym") do
MILLION_OF_JSONS.each { |json| JSON.parse(json, symbolize_names: true)[:data] }
end
x.report("Jsoncons MILLION_OF_JSONS sym") do
MILLION_OF_JSONS.each { |json| Jsoncons::Json.parse(json)[:data] }
end
end
Benchmark.bm(35) do |x|
x.report("JSON MILLION_OF_JSONS str") do
MILLION_OF_JSONS.each { |json| JSON.parse(json)['data'] }
end
x.report("Jsoncons MILLION_OF_JSONS str") do
MILLION_OF_JSONS.each { |json| Jsoncons::Json.parse(json)['data'] }
end
end
Benchmark.bm(35) do |x|
x.report("JSON MILLION_OF_ELEMENTS sym") do
10.times { JSON.parse(MILLION_OF_ELEMENTS, symbolize_names: true)[:data] }
end
x.report("Jsoncons MILLION_OF_ELEMENTS sym") do
10.times { Jsoncons::Json.parse(MILLION_OF_ELEMENTS)[:data] }
end
end
HUNDRED_LEVELS_JSON = '{"data":' * 100 + '1' + '}' * 100
DATA_SYM = :data
DATA_STR = 'data'
Benchmark.bm(35) do |x|
json = JSON.parse(HUNDRED_LEVELS_JSON, symbolize_names: true)
x.report("JSON HUNDRED_LEVELS_JSON sym") do
100_000.times do
data = json
100.times { data = data[DATA_SYM] }
end
end
json = Jsoncons::Json.parse(HUNDRED_LEVELS_JSON)
x.report("Jsoncons HUNDRED_LEVELS_JSON sym") do
100_000.times do
data = json
100.times { data = data[DATA_SYM] }
end
end
end
Benchmark.bm(35) do |x|
json = JSON.parse(HUNDRED_LEVELS_JSON)
x.report("JSON HUNDRED_LEVELS_JSON str") do
100_000.times do
data = json
100.times { data = data[DATA_STR] }
end
end
json = Jsoncons::Json.parse(HUNDRED_LEVELS_JSON)
x.report("Jsoncons HUNDRED_LEVELS_JSON str") do
100_000.times do
data = json
100.times { data = data[DATA_STR] }
end
end
end