public
Last active

Python JSON Benchmark

  • Download Gist
benchmark.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
'''cjson, jsonlib, simplejson, and yajl also use C code
demjson did not use C code, but was too painfully slow to benchmark
(took about 20 seconds for these tests)
 
'''
import json
import sys
import time
 
with open('doc.json') as f:
decoded = f.read()
encoded = json.loads(decoded)
 
def test_encodes(modules):
encode_times = {}
for json_module in modules:
start = time.clock()
for _ in xrange(10000):
json_module.loads(decoded)
 
total_time = time.clock() - start
assert json_module.loads(decoded) == encoded
encode_times[json_module.__name__] = total_time
return encode_times
 
def test_decodes(modules):
decode_times = {}
 
for json_module in modules:
start = time.clock()
 
for _ in xrange(10000):
json_module.dumps(encoded)
 
total_time = time.clock() - start
assert json.loads(json_module.dumps(encoded)) == encoded
decode_times[json_module.__name__] = total_time
return decode_times
 
def printer(mapping):
''' mapping maps module name to time'''
for k, v in mapping.iteritems():
print '%s: %ss' % (k, v)
 
 
def main():
print 'JSON Benchmark'
try:
import __pypy__
except ImportError:
# we are using CPython
print sys.version
import cjson, jsonlib, simplejson, ujson, yajl
 
jsonlib.loads = jsonlib.read
jsonlib.dumps = jsonlib.write
cjson.loads = cjson.decode
cjson.dumps = cjson.encode
modules = [cjson, json, jsonlib, simplejson, ujson, yajl]
else:
# we are using PyPy
print sys.version
modules = [json]
print '-----------------------------'
print 'ENCODING'
printer(test_encodes(modules))
print ''
print 'DECODING'
printer(test_decodes(modules))
 
if __name__ == '__main__':
main()
doc.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
{
"web-app": {
"servlet": [
{
"servlet-name": "cofaxCDS",
"servlet-class": "org.cofax.cds.CDSServlet",
"init-param": {
"configGlossary:installationAt": "Philadelphia, PA",
"configGlossary:adminEmail": "ksm@pobox.com",
"configGlossary:poweredBy": "Cofax",
"configGlossary:poweredByIcon": "/images/cofax.gif",
"configGlossary:staticPath": "/content/static",
"templateProcessorClass": "org.cofax.WysiwygTemplate",
"templateLoaderClass": "org.cofax.FilesTemplateLoader",
"templatePath": "templates",
"templateOverridePath": "",
"defaultListTemplate": "listTemplate.htm",
"defaultFileTemplate": "articleTemplate.htm",
"useJSP": false,
"jspListTemplate": "listTemplate.jsp",
"jspFileTemplate": "articleTemplate.jsp",
"cachePackageTagsTrack": 200,
"cachePackageTagsStore": 200,
"cachePackageTagsRefresh": 60,
"cacheTemplatesTrack": 100,
"cacheTemplatesStore": 50,
"cacheTemplatesRefresh": 15,
"cachePagesTrack": 200,
"cachePagesStore": 100,
"cachePagesRefresh": 10,
"cachePagesDirtyRead": 10,
"searchEngineListTemplate": "forSearchEnginesList.htm",
"searchEngineFileTemplate": "forSearchEngines.htm",
"dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
"dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
"dataStoreUser": "sa",
"dataStorePassword": "dataStoreTestQuery",
"dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
"dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
"dataStoreInitConns": 10,
"dataStoreMaxConns": 100,
"dataStoreConnUsageLimit": 100,
"dataStoreLogLevel": "debug",
"maxUrlLength": 500
}
},
{
"servlet-name": "cofaxEmail",
"servlet-class": "org.cofax.cds.EmailServlet",
"init-param": {
"mailHost": "mail1",
"mailHostOverride": "mail2"
}
},
{
"servlet-name": "cofaxAdmin",
"servlet-class": "org.cofax.cds.AdminServlet"
},
{
"servlet-name": "fileServlet",
"servlet-class": "org.cofax.cds.FileServlet"
},
{
"servlet-name": "cofaxTools",
"servlet-class": "org.cofax.cms.CofaxToolsServlet",
"init-param": {
"templatePath": "toolstemplates/",
"log": 1,
"logLocation": "/usr/local/tomcat/logs/CofaxTools.log",
"logMaxSize": "",
"dataLog": 1,
"dataLogLocation": "/usr/local/tomcat/logs/dataLog.log",
"dataLogMaxSize": "",
"removePageCache": "/content/admin/remove?cache=pages&id=",
"removeTemplateCache": "/content/admin/remove?cache=templates&id=",
"fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder",
"lookInContext": 1,
"adminGroupID": 4,
"betaServer": true
}
}
],
"servlet-mapping": {
"cofaxCDS": "/",
"cofaxEmail": "/cofaxutil/aemail/*",
"cofaxAdmin": "/admin/*",
"fileServlet": "/static/*",
"cofaxTools": "/tools/*"
},
"taglib": {
"taglib-uri": "cofax.tld",
"taglib-location": "/WEB-INF/tlds/cofax.tld"
}
}
}
requirements.txt
1 2 3 4 5
jsonlib==1.6.1
python-cjson==1.0.5
simplejson==2.1.6
ujson==1.4
yajl==0.3.5
results
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
CPython
JSON Benchmark
2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2]
-----------------------------
ENCODING
simplejson: 0.45s
cjson: 0.45s
ujson: 0.46s
jsonlib: 0.65s
json: 1.02s
yajl: 0.7s
 
DECODING
simplejson: 0.73s
cjson: 0.83s
ujson: 0.41s
jsonlib: 0.48s
json: 0.6s
yajl: 0.59s
 
PyPy
JSON Benchmark
2.7.1 (b590cf6de419, Apr 30 2011, 02:00:38)
[PyPy 1.5.0-alpha0 with GCC 4.4.3]
-----------------------------
ENCODING
json: 2.839568s
 
DECODING
json: 5.551157s

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.