Skip to content

Instantly share code, notes, and snippets.

@chuyeow
Created December 1, 2008 04:08
Show Gist options
  • Save chuyeow/30614 to your computer and use it in GitHub Desktop.
Save chuyeow/30614 to your computer and use it in GitHub Desktop.
Is JSON.parse! faster?
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
user system total real
json:parse 0.100000 0.010000 0.110000 ( 0.106557)
json:parse! 0.110000 0.000000 0.110000 ( 0.129599)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment