Created
October 15, 2018 18:59
-
-
Save graphaelli/57142623ea7c7364b38e851e9e4b7af2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
import timeit | |
import json | |
args = dict(ensure_ascii=False) | |
def dump_and_write(obj, fp): | |
fp.write(json.dumps(obj, **args)) | |
def dump(obj, fp): | |
json.dump(obj, fp, **args) | |
def benchit(fn): | |
setup = """from __main__ import {} as d | |
import codecs | |
from io import StringIO | |
out = StringIO() | |
# out = codecs.open("{}" + "out", mode="w", encoding="utf-8") | |
""".format(fn, fn) | |
print(fn, timeit.timeit('d({"foo": "bar"}, out)', setup=setup)) | |
if __name__ == '__main__': | |
benchit(dump_and_write.__name__) | |
benchit(dump.__name__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment