Skip to content

Instantly share code, notes, and snippets.

@andreif
Forked from schlamar/gist:5373110
Last active December 20, 2015 18:28
Show Gist options
  • Select an option

  • Save andreif/6175681 to your computer and use it in GitHub Desktop.

Select an option

Save andreif/6175681 to your computer and use it in GitHub Desktop.
import time
import json
import ujson
import cjson
import yajl
import simplejson
DATA = [{
'words': """
Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Mauris adipiscing adipiscing placerat.
Vestibulum augue augue,
pellentesque quis sollicitudin id, adipiscing.
""",
'list': range(50),
'dict': dict((str(i), 'a') for i in xrange(50)),
'int': 100,
'float': 100.123456
}]
for N, data in [(5, 0), (3, 2), (2, 3)]:
print '\n-- N=1e%s, len(text)=%s\n' % (N, len(ujson.dumps(DATA)) * 10 ** data)
N = 10 ** N
data = DATA * 10 ** data
ref = None
for j in [ujson, cjson, yajl, simplejson, json]:
dts = []
f = j.encode if j == cjson else j.dumps
for i in range(0, N):
d = list(data)
t = time.time()
s = f(d)
t = time.time() - t
dts.append(t)
f = j.decode if j == cjson else j.loads
for i in range(0, N):
t = time.time()
x = f(s)
t = time.time() - t
dts.append(t)
tot = [sum(dts[:N]), sum(dts[N:])]
if not ref:
ref = tot
print '%10s %.1f %.1f' % (f.__module__, tot[0] / ref[0], tot[1] / ref[1])
"""
-- N=1e5, len(text)=854
ujson 1.0 1.0
cjson 2.6 1.9
yajl 2.6 1.9
simplejson 4.4 2.0
json 3.4 4.2
-- N=1e5, len(text)=8540
ujson 1.0 1.0
cjson 3.1 1.8
yajl 2.9 1.7
simplejson 3.5 1.7
json 3.2 4.5
-- N=1e3, len(text)=85400
ujson 1.0 1.0
cjson 2.7 1.5
yajl 2.6 1.5
simplejson 2.9 1.2
json 3.0 3.6
-- N=1e2, len(text)=854000
ujson 1.0 1.0
cjson 2.7 1.4
yajl 2.6 1.5
simplejson 3.2 1.1
json 3.3 3.5
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment