Created
October 29, 2023 23:33
-
-
Save serjflint/9adcc693be324adb946c473661bbc7a0 to your computer and use it in GitHub Desktop.
bench json
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
import json | |
import orjson | |
import pickle | |
import time | |
import contextlib | |
DATA = [] | |
@contextlib.contextmanager | |
def bench(name): | |
start = time.perf_counter() | |
try: | |
yield | |
finally: | |
end = time.perf_counter() | |
print(f'{name}: {end-start}') | |
with open('/home/serjflint/workspace/cache.json') as stream: | |
with bench('load'): | |
for line in stream: | |
DATA.append(json.loads(line)) | |
DATA = DATA * 10 | |
with open('/tmp/json.json', mode='w') as stream: | |
with bench('json'): | |
json.dump(DATA, stream) | |
with open('/tmp/orjson.json', mode='wb') as stream: | |
with bench('orjson'): | |
stream.write(orjson.dumps(DATA)) | |
with open('/tmp/pickle.json', mode='wb') as stream: | |
with bench('pickle'): | |
pickle.dump(DATA, stream) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
load: 0.6993525180005236
json: 14.33412618699731
orjson: 0.9688011159996677
pickle: 0.5945636799988279