Skip to content

Instantly share code, notes, and snippets.

@sekimura
Created February 6, 2012 20:14
Show Gist options
  • Save sekimura/1754535 to your computer and use it in GitHub Desktop.
Save sekimura/1754535 to your computer and use it in GitHub Desktop.
from timeit import Timer
def test_json_response():
new_json_response = """\
from django.http import HttpResponse
from django.utils import simplejson
class JsonResponse(HttpResponse):
def __init__(self, data=None, content_type=None, **kwargs):
if not content_type:
content_type = 'application/json; charset=utf-8'
content = simplejson.dumps(data)
super(JsonResponse, self).__init__(
content,
content_type=content_type,
**kwargs
)
data = []
for i in range(0, 100):
data.append({'key': 'value', 'int': 42, 'a': [1, 'hello world']})
resp = JsonResponse(data)
"""
t = Timer(stmt=new_json_response)
print "new JsonResponse %.2f usec/pass" % (1000000 * t.timeit(number=10000)/10000)
old_json_response = """\
from django.http import HttpResponse
from django.utils import simplejson
class JsonResponse(HttpResponse):
def __init__(self, data=None, status=None, content_type=None, **kwargs):
if not content_type:
content_type = 'application/json; charset=utf-8'
HttpResponse.__init__(self, '', status=status,
content_type=content_type)
simplejson.dump(data, self, **kwargs)
data = []
for i in range(0, 100):
data.append({'key': 'value', 'int': 42, 'a': [1, 'hello world']})
resp = JsonResponse(data)
"""
t = Timer(stmt=old_json_response)
print "old JsonResponse %.2f usec/pass" % (1000000 * t.timeit(number=10000)/10000)
if __name__ == '__main__':
test_json_response()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment