Skip to content

Instantly share code, notes, and snippets.

@draganHR
Last active March 25, 2020 09:25
Show Gist options
  • Save draganHR/e5f1bd32e831c2f6331aecfe390bc1dc to your computer and use it in GitHub Desktop.
Save draganHR/e5f1bd32e831c2f6331aecfe390bc1dc to your computer and use it in GitHub Desktop.
benchmark_json.py
from __future__ import unicode_literals, print_function
import time
import json
import simplejson
import ujson
import msgpack
def get_example_raw_data():
rows = [
{
"timestamp": 23437245.353654523 + i,
"task_uuid": "4e711429-4a6a-4cde-8762-%012d" % i,
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window_%s" % i,
"width": 500 + i,
"height": 500 + i * 3
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250 + i,
"vOffset": 250 + i * 3,
"alignment": "center"
},
"text": {
"data": "Click Here (%s)" % i,
"size": 36,
"style": "bold",
"name": "text%s" % i,
"hOffset": 250 + i,
"vOffset": 100 + i * 3,
"alignment": "center",
"onMouseUp": "sun%s.opacity = (sun%s.opacity / 100) * 90;" % (i, i)
},
} for i in range(100)
]
example_raw_data = {
'id': 1,
'rows': rows,
}
return example_raw_data
def benchmark_func(name, f, *args):
iterations = 10000
start = time.time()
for _ in range(iterations):
f(*args)
print(name, time.time() - start)
def main():
example_raw_data = get_example_raw_data()
example_json_data = json.dumps(example_raw_data)
example_msgpack_data = msgpack.packb(example_raw_data)
benchmark_func("json.dumps", json.dumps, example_raw_data)
benchmark_func("simplejson.dumps", simplejson.dumps, example_raw_data)
benchmark_func("ujson.dumps", ujson.dumps, example_raw_data)
benchmark_func("msgpack.packb", msgpack.packb, example_raw_data)
benchmark_func("json.loads", json.loads, example_json_data)
benchmark_func("simplejson.loads", simplejson.loads, example_json_data)
benchmark_func("ujson.loads", ujson.loads, example_json_data)
benchmark_func("msgpack.unpackb", msgpack.unpackb, example_msgpack_data)
if __name__ == '__main__':
main()

i7-8650U

python.x86_64 2.7.5-86.el7 python2-simplejson.x86_64 3.10.0-2.el7 python-ujson.x86_64 1.35-1.el7 msgpack 0.5.6

json.dumps 6.72731995583
simplejson.dumps 8.35752797127
ujson.dumps 4.401034832
msgpack.packb 4.03916501999
json.loads 14.0235741138
simplejson.loads 4.65079498291
ujson.loads 4.1893351078
msgpack.unpackb 2.6899638176

i7-8650U

python3.x86_64 3.6.8-10.el7 python36-simplejson.x86_64 3.10.0-2.el7 ujson 2.0.3 python36-msgpack.x86_64 0.5.6-5.el7

json.dumps 6.7075865268707275
simplejson.dumps 8.3774094581604
ujson.dumps 3.070307970046997
msgpack.packb 1.9636342525482178
json.loads 5.397673606872559
simplejson.loads 5.884173393249512
ujson.loads 4.301633358001709
msgpack.unpackb 2.628566265106201
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment