Skip to content

Instantly share code, notes, and snippets.

@methane
Last active Aug 29, 2015
Embed
What would you like to do?
from __future__ import print_function
import json
bdata = b'{"foo0": "bar0","foo1": "bar1","foo2": "bar2","foo3": "bar3","foo4": "bar4","foo5": "bar5","foo6": "bar6"}'
udata = u'{"foo0": "bar0","foo1": "bar1","foo2": "bar2","foo3": "bar3","foo4": "bar4","foo5": "bar5","foo6": "bar6"}'
def decode_b():
json.loads(bdata)
def decode_u():
json.loads(udata)
def decode_x():
if isinstance(udata, unicode):
x = udata.encode('utf-8')
else:
x = udata
json.loads(x)
import timeit
def bench():
print("bytes: ", timeit.timeit(decode_b, number=100000))
print("unicode: ", timeit.timeit(decode_u, number=100000))
print("pre encode: ", timeit.timeit(decode_x, number=100000))
for _ in range(10):
bench()
$ ~/local/pypy-2.6.0-osx64/bin/pypy bench.py
bytes: 0.121253013611
unicode: 0.51861000061
pre encode: 0.186152935028
bytes: 0.13639998436
unicode: 0.485965013504
pre encode: 0.139013051987
bytes: 0.105733156204
unicode: 0.441829919815
pre encode: 0.130655050278
bytes: 0.110300064087
unicode: 0.454895019531
pre encode: 0.13870716095
bytes: 0.108355045319
unicode: 0.525803804398
pre encode: 0.164574146271
bytes: 0.146996974945
unicode: 0.561616182327
pre encode: 0.157838821411
bytes: 0.123056173325
unicode: 0.532580137253
pre encode: 0.152902841568
bytes: 0.122449874878
unicode: 0.563801050186
pre encode: 0.170814990997
bytes: 0.104251861572
unicode: 0.508492946625
pre encode: 0.177821874619
bytes: 0.12731218338
unicode: 0.492110967636
pre encode: 0.137986898422
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment