Skip to content

Instantly share code, notes, and snippets.

@eamartin
Created August 10, 2011 09:10
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save eamartin/1136415 to your computer and use it in GitHub Desktop.
Save eamartin/1136415 to your computer and use it in GitHub Desktop.
Python JSON Benchmark
'''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()
{
"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"
}
}
}
jsonlib==1.6.1
python-cjson==1.0.5
simplejson==2.1.6
ujson==1.4
yajl==0.3.5
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment