Last active
August 29, 2015 14:08
-
-
Save Rhomboid/f3ea24afaf0f1bf594fa to your computer and use it in GitHub Desktop.
Python JSON module benchmark (3.x)
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
from random import randint, random, choice, uniform, seed | |
from string import ascii_letters | |
from timeit import repeat | |
import json | |
def rand_string(): | |
return ''.join(choice(ascii_letters) for _ in range(randint(3, 24))) | |
def make_test_data(depth=0): | |
if random() > 1 - (depth / 9)**3: | |
if random() > 0.75: | |
return rand_string() | |
else: | |
return round(uniform(-1e6, 1e6), 4) | |
else: | |
seq = range(randint(3, 7)) | |
if randint(0, 1): | |
return [make_test_data(depth + 1) for _ in seq] | |
else: | |
return {rand_string(): make_test_data(depth + 1) for _ in seq} | |
seed(0xfeedbeef) | |
data = make_test_data() | |
data_json = json.dumps(data) | |
data_len = len(data_json) | |
prep = 'import json, io; from __main__ import data, data_json; outfile = io.StringIO(); infile = io.StringIO(data_json)' | |
tests = 'json.dump(data, outfile)', 'outfile.write(json.dumps(data))', 'json.load(infile)', 'json.loads(infile.read())' | |
for time, test in sorted((min(repeat(test, prep, repeat=5, number=1)), test) for test in tests): | |
print('{:40} {:7.3f} MB/s'.format(test, data_len / 1024**2 / time)) |
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
json.loads(infile.read()) 42.335 MB/s | |
json.load(infile) 42.240 MB/s | |
outfile.write(json.dumps(data)) 23.574 MB/s | |
json.dump(data, outfile) 5.196 MB/s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment