Skip to content

@mikeal /

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
#!/usr/bin/env python
from datetime import datetime
import os, sys
import json
import simplejson
import cjson
import jsonlib2
small_doc_string = '{"type":"counting"}'
large_doc_string = open(os.path.join(os.path.dirname(__file__), 'large_doc.json'), 'r').read()
small_doc_dict = {'type':'counting'}
large_doc_dict = json.loads(large_doc_string)
# to_seconds_float from
def to_seconds_float(timedelta):
"""Calculate floating point representation of combined
seconds/microseconds attributes in :param:`timedelta`.
:raise ValueError: If :param:`timedelta.days` is truthy.
>>> to_seconds_float(datetime.timedelta(seconds=1, milliseconds=500))
>>> too_big = datetime.timedelta(days=1, seconds=12)
>>> to_seconds_float(too_big) # doctest: +ELLIPSIS
Traceback (most recent call last):
ValueError: ('Must not have days', datetime.timedelta(1, 12))
if timedelta.days:
raise ValueError('Must not have days', timedelta)
return timedelta.seconds + timedelta.microseconds / 1E6
class ComparisonTimer(object):
def __init__(self, func, assertion=None):
self.func = func
self.timers = []
self.assertion = assertion
def run(self):
starttime =
result = self.func()
endtime =
if self.assertion is None:
assert result
assert result == self.assertion
self.timers.append(endtime - starttime)
def sum(self):
return sum([to_seconds_float(t) for t in self.timers]) / len(self.timers)
def repeat(self, number):
for x in range(number):
return sum([to_seconds_float(t) for t in self.timers])
def test(func, assertion=None, repeat=100):
sys.stdout.write('.'); sys.stdout.flush();
return ComparisonTimer(func, assertion).repeat(repeat)
def run_test(store, func_dict, arg, assertion):
for key, func in func_dict.items():
store[key].append(test(lambda : func(arg), assertion))
def run():
parse_times = {'json':[],'jsonlib2':[],'simplejson':[],'cjson':[]}
dump_times = {'json':[],'jsonlib2':[],'simplejson':[],'cjson':[]}
parse_func_dict = {'json':json.loads, 'jsonlib2':jsonlib2.loads,
'simplejson':simplejson.loads, 'cjson':cjson.decode}
dump_func_dict = {'json':json.dumps, 'jsonlib2':jsonlib2.dumps,
'simplejson':simplejson.dumps, 'cjson':cjson.encode}
for x in range(100):
run_test(parse_times, parse_func_dict, large_doc_string, large_doc_dict)
run_test(dump_times, dump_func_dict, large_doc_dict, None)
print ''
for k, v in parse_times.items():
print 'ParseTime:', k, str(sum(v) / len(v) * 1000)+'ms'
for k, v in dump_times.items():
print 'DumpTime:', k, str(sum(v) / len(v) * 1000)+'ms'
if __name__ == "__main__":
except Exception, e:
import pdb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.