# -*-coding:utf-8-*- '''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 # Compact between Python literal and JSON format. false = False true = True 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, "decoded:%s, encoded:%s" % (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_module.loads(json_module.dumps(encoded)) == encoded, "decoded:%s, encoded:%s" % (decoded, 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) import cjson import jsonlib import simplejson import ujson import yajl jsonlib.loads = jsonlib.read jsonlib.dumps = jsonlib.write cjson.loads = cjson.decode cjson.dumps = cjson.encode from bunch import Bunch python = Bunch({"loads": eval, "dumps": str, "__name__": "Python"}) import jsonpickle # jsonpickle.load_backend('ujson', 'dumps', 'loads', ValueError) jsonpickle.load_backend('simplejson', 'dumps', 'loads', ValueError) jsonpickle.set_preferred_backend('ujson') jsonpickle.loads = jsonpickle.decode jsonpickle.dumps = jsonpickle.encode try: import cPickle as pickle except: import pickle common_data_type_between_python_and_json = set([str, unicode, int, list, dict, float, bool, type(None)]) class PythonObjectEncoder_json(json.JSONEncoder): # copied from http://stackoverflow.com/questions/8230315/python-sets-are-not-json-serializable def default(self, obj): if isinstance(obj, common_data_type_between_python_and_json): return json.JSONEncoder.default(self, obj) return {'__po__': pickle.dumps(obj)} class PythonObjectEncoder_simplejson(simplejson.JSONEncoder): def default(self, obj): if isinstance(obj, common_data_type_between_python_and_json): return simplejson.JSONEncoder.default(self, obj) return {'__po__': pickle.dumps(obj)} def as_python_object(dct): if '__po__' in dct: return pickle.loads(str(dct['__po__'])) return dct json_with_pickle = Bunch({ "loads": lambda value: json.loads(value, object_hook=as_python_object), "dumps": lambda value: json.dumps(value, cls=PythonObjectEncoder_json), "__name__": "json_with_pickle"}) simplejson_with_pickle = Bunch({ # "loads": lambda value: simplejson.loads(value, object_hook=as_python_object), "loads": lambda value: json.loads(value, object_hook=as_python_object), # json is faster "dumps": lambda value: simplejson.dumps(value, cls=PythonObjectEncoder_simplejson), "__name__": "simplejson_with_pickle"}) def main(): print 'JSON Benchmark' try: import __pypy__ __pypy__ # follow PEP8, make __pypy__ used. except ImportError: # we are using CPython print sys.version.strip() modules = [cjson, json, jsonlib, simplejson, ujson, yajl, python, jsonpickle, json_with_pickle, simplejson_with_pickle] else: # we are using PyPy print sys.version modules = [json] print '-----------------------------' print 'LOADS' printer(test_encodes(modules)) print '' print 'DUMPS' printer(test_decodes(modules)) if __name__ == '__main__': main()