Skip to content

Instantly share code, notes, and snippets.

@hktechn0
Created January 8, 2013 18:52
Show Gist options
  • Save hktechn0/4486729 to your computer and use it in GitHub Desktop.
Save hktechn0/4486729 to your computer and use it in GitHub Desktop.
json serialize benchmark on python
import time
import random
import uuid
import json
import simplejson
import cjson
import czjson
import jsonlib
import jsonlib2
import demjson
import anyjson
import ujson
if __name__ == "__main__":
sample = [[random.randint(0, 0xffffff),
random.randint(0, 0xffffff),
random.randint(0, 0xffffff),
random.randint(0, 0xffffff),
random.randint(0, 0xffffff),
random.randint(0, 0xffffff),
uuid.uuid4().hex]
for i in range(500000)]
c = time.clock()
json.dumps(sample)
c = time.clock() - c
print "json:", c
c = time.clock()
simplejson.dumps(sample)
c = time.clock() - c
print "simplejson:", c
c = time.clock()
cjson.encode(sample)
c = time.clock() - c
print "cjson:", c
c = time.clock()
czjson.dumps(sample)
c = time.clock() - c
print "czjson:", c
c = time.clock()
jsonlib.write(sample)
c = time.clock() - c
print "jsonlib:", c
c = time.clock()
jsonlib2.write(sample)
c = time.clock() - c
print "jsonlib2:", c
c = time.clock()
demjson.encode(sample)
c = time.clock() - c
print "demjson:", c
c = time.clock()
anyjson.dumps(sample)
c = time.clock() - c
print "anyjson:", c
c = time.clock()
ujson.dumps(sample)
c = time.clock() - c
print "ujson:", c
### Result
# simplejson==3.0.5
# python-cjson==1.0.5
# czjson==1.0.8
# jsonlib==1.6.1
# jsonlib2==1.5.2
# demjson==1.6
# anyjson==0.3.3
# ujson==1.23
#
## python 2.7 (on Local Core2 Duo E8400)
# json: 0.79
# simplejson: 0.92
# cjson: 0.85
# czjson: 0.85
# jsonlib: 1.17
# jsonlib2: 0.96
# demjson: 31.77
# anyjson: 1.12
# ujson: 0.43
#
## python2.6 (on Sakura VPS)
# json: 11.42
# simplejson: 1.67
# cjson: 1.43
# czjson: 1.42
# jsonlib: 1.68
# jsonlib2: 1.82
# demjson: 45.68
# anyjson: 1.91
# ujson: 0.54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment