Created
December 31, 2019 18:53
-
-
Save darrenboyd/463f45e095db813ed8fad31fb9ffb401 to your computer and use it in GitHub Desktop.
A simple benchmark script to compare json and simplejson
This file contains 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 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