Skip to content

Instantly share code, notes, and snippets.

@voluntas
Forked from eamartin/benchmark.py
Last active March 6, 2021 18:59
Show Gist options
  • Save voluntas/05482e373baad5d769eb to your computer and use it in GitHub Desktop.
Save voluntas/05482e373baad5d769eb to your computer and use it in GitHub Desktop.
PyPy での JSON ライブラリベンチーマーク
'''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
import simplejson
print sys.version
modules = [json, simplejson]
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": "[email protected]",
"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.1.0
simplejson==3.6.5
ujson==1.33
yajl==0.3.5
CPython
JSON Benchmark
2.7.9 (default, Dec 11 2014, 02:36:08)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)]
-----------------------------
ENCODING
simplejson: 0.281742s
cjson: 0.483193s
ujson: 0.21507s
jsonlib: 0.409993s
json: 0.732388s
yajl: 0.395648s
DECODING
simplejson: 0.540883s
cjson: 0.356611s
ujson: 0.215889s
jsonlib: 0.258306s
json: 0.35897s
yajl: 0.394774s
PyPy
JSON Benchmark
2.7.8 (10f1b29a2bd21f837090286174a9ca030b8680b2, Feb 05 2015, 07:09:36)
[PyPy 2.5.0 with GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)]
-----------------------------
ENCODING
json: 0.176226s
simplejson: 1.230522s
DECODING
json: 0.342462s
simplejson: 2.074411s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment