chuyeow (owner)

Revisions

gist: 30614 Download_button fork
public
Description:
Is JSON.parse! faster?
Public Clone URL: git://gist.github.com/30614.git
Embed All Files: show embed
json_parse_bang.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
require 'rubygems'
require 'json/ext'
require 'benchmark'
 
N = 1000
 
JSON_STRING = <<-EOS
{"searchRequest": {
"id": 0,
"pickUpDate": "2008-12-22 12:00:00.0 GMT+08:00",
"dropOffDate": "2008-12-28 12:00:00.0 GMT+08:00",
"sameDropOffLocation": true,
"pickUp": {
"id": 7046,
"country": {
"id": 14,
"code": "AU",
"name": "Australia"
},
"state": {
"id": 52,
"code": "NSW",
"name": "New South Wales"
},
"code": "SYD",
"name": "Sydney",
"locationType": "CITY"
},
"dropOff": {
"id": 7046,
"country": {
"id": 14,
"code": "AU",
"name": "Australia"
},
"state": {
"id": 52,
"code": "NSW",
"name": "New South Wales"
},
"code": "SYD",
"name": "Sydney",
"locationType": "CITY"
},
"searchedDate": "2008-12-01 11:21:30.929 GMT+08:00",
"countryOfResidence": {
"id": 14,
"code": "AU",
"name": "Australia"
},
"userAge": 27,
"providerList": [
"europcar.com",
"hertz.com"
],
"instanceId": "bade2c0442450cc3788fe3264db7a30dead4e275"
}}
EOS
 
Benchmark.bm do |x|
  x.report('json:parse') do
    N.times do
      JSON.parse(JSON_STRING)
    end
  end
 
  x.report('json:parse!') do
    N.times do
      JSON.parse!(JSON_STRING)
    end
  end
end
Results #
1
2
3
      user system total real
json:parse 0.100000 0.010000 0.110000 ( 0.106557)
json:parse! 0.110000 0.000000 0.110000 ( 0.129599)