Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 76 lines (61 sloc) 1.82 KB
import time
###
### Test Environment
###
import cPickle
import simplejson
import json
import cjson # python-cjson in pypi
import ujson
import tnetstrings # http://tnetstrings.org/tnetstrings.py
import tnetstring
import msgpack # msgpack-python
### Make cjson's interface match the others
cjson.dumps = cjson.encode
cjson.loads = cjson.decode
### Same with tnetstring
tnetstrings.dumps = tnetstrings.dump
tnetstrings.loads = tnetstrings.parse
### And msgpack too
msgpack.dumps = msgpack.packb
msgpack.loads = msgpack.unpackb
### Prepare data structures
data_as_dict = {
'foo': 'bar',
'food': 'barf',
'good': 'bars',
'dood': 'wheres your car?',
'wheres your car': 'dude?',
}
data_as_json = json.dumps(data_as_dict)
data_as_pickled = cPickle.dumps(data_as_dict)
data_as_tnetstrings = tnetstrings.dumps(data_as_dict)
data_as_tnetstring = data_as_tnetstrings
data_as_msgpack = msgpack.dumps(data_as_dict)
###
num_loop_iterations = 1000000
### List of JSON implementations to test
mods = [json, simplejson, cjson, ujson, cPickle, tnetstrings, tnetstring, msgpack]
### Time dumping from native to JSON
print 'Dumping:'
for mod in mods:
start = time.time()
for i in xrange(num_loop_iterations):
mod.dumps(data_as_dict)
print ' %s: %s' % (mod.__name__, (time.time() - start))
### Time loading from JSON into native
print 'Loading:'
for mod in mods:
data = data_as_json
if mod.__name__ == "cPickle":
data = data_as_pickled
elif mod.__name__ == "tnetstrings":
data = data_as_tnetstrings
elif mod.__name__ == "tnetstring":
data = data_as_tnetstring
elif mod.__name__ == "msgpack":
data = data_as_msgpack
start = time.time()
for i in xrange(num_loop_iterations):
mod.loads(data)
print ' %s: %s' % (mod.__name__, (time.time() - start))
Something went wrong with that request. Please try again.