Created
November 20, 2009 23:56
-
-
Save mikeal/239887 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
# http://stackoverflow.com/questions/1083402/missing-datetime-timedelta-toseconds-float-in-python | |
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)) | |
1.5 | |
>>> 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 = datetime.now() | |
result = self.func() | |
endtime = datetime.now() | |
if self.assertion is None: | |
assert result | |
else: | |
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): | |
self.run() | |
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__": | |
try: | |
run() | |
except Exception, e: | |
import pdb | |
pdb.post_mortem() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment