Skip to content

Instantly share code, notes, and snippets.

@serjflint
Created October 29, 2023 23:33
Show Gist options
  • Save serjflint/9adcc693be324adb946c473661bbc7a0 to your computer and use it in GitHub Desktop.
Save serjflint/9adcc693be324adb946c473661bbc7a0 to your computer and use it in GitHub Desktop.
bench json
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)
@serjflint
Copy link
Author

load: 0.6993525180005236
json: 14.33412618699731
orjson: 0.9688011159996677
pickle: 0.5945636799988279

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment