Tuesday, September 17, 2013

Comparison of python json libs in performance

I work a lot with json in python and never thought before about performance. I was using standard python json lib that goes right from the box, but today found out from one of my colleagues that there is way more faster lib called cjson. I got interested in this and googled a little bit. As a result i got a python script that shows which json lib is faster on Python. Here it is :
import time
import pickle
import yajl
try:
import cjson
except ImportError:
cjson = None
try:
import simplejson
except ImportError:
simplejson = None
try:
import json
except ImportError:
json = None
try:
import ujson
except ImportError:
ujson = None
default_data = {
"name": "Foo",
"type": "Bar",
"count": 1,
"info": {
"x": 203,
"y": 102,},}
def ttt(f, data=None, x=100*1000):
start = time.time()
while x:
x -= 1
foo = f(data)
return time.time()-start
def profile(serial, deserial, data=None, x=100*1000):
if not data:
data = default_data
squashed = serial(data)
return (ttt(serial, data, x), ttt(deserial, squashed, x))
def test(serial, deserial, data=None):
if not data:
data = default_data
assert deserial(serial(data)) == data
contenders = [
('yajl', (yajl.Encoder().encode, yajl.Decoder().decode)),
]
if json:
contenders.append(('json', (json.dumps, json.loads)))
if cjson:
contenders.append(('cjson', (cjson.encode, cjson.decode)))
if simplejson:
contenders.append(('simplejson', (simplejson.dumps, simplejson.loads)))
if json:
contenders.append(('stdlib json', (json.dumps, json.loads)))
if ujson:
contenders.append(('ultra json', (ujson.dumps, ujson.loads)))
for name, args in contenders:
test(*args)
x, y = profile(*args)
print("%-11s serialize: %0.3f deserialize: %0.3f total: %0.3f" % (
name, x, y, x+y))
As a result i got this metrics :
yajl        serialize: 0.308  deserialize: 0.334  total: 0.642
json        serialize: 1.245  deserialize: 1.384  total: 2.629
cjson       serialize: 0.339  deserialize: 0.244  total: 0.583
stdlib json serialize: 1.019  deserialize: 1.396  total: 2.415
ultra json  serialize: 0.170  deserialize: 0.159  total: 0.328
[Finished in 7.0s]
And the winner is ultra json! :) If you want to clone this script for yourself - here's the link  

No comments:

Post a Comment