Skip to content

Instantly share code, notes, and snippets.

@darrenboyd
Created December 31, 2019 18:53
Show Gist options
  • Save darrenboyd/463f45e095db813ed8fad31fb9ffb401 to your computer and use it in GitHub Desktop.
Save darrenboyd/463f45e095db813ed8fad31fb9ffb401 to your computer and use it in GitHub Desktop.
A simple benchmark script to compare json and simplejson
import sys
import os
import timeit
import json
import simplejson
CONTENTS = []
def test_json():
for c in CONTENTS:
json.loads(c)
def test_simplejson():
for c in CONTENTS:
simplejson.loads(c)
def run_benchmark(json_dir):
for fname in os.listdir(json_dir):
_, ext = os.path.splitext(fname)
if ext != ".json":
continue
with open(os.path.join(json_dir, fname)) as f:
CONTENTS.append(f.read())
# JSON
setup = "from __main__ import test_json"
res = timeit.timeit("test_json()", setup=setup, number=10)
print(f" JSON: {res}")
# SIMPLEJSON
setup = "from __main__ import test_simplejson"
res = timeit.timeit("test_simplejson()", setup=setup, number=10)
print(f"SIMPLEJSON: {res}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <dir with containing json>")
exit(1)
else:
run_benchmark(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment