Created
April 20, 2015 09:10
-
-
Save ttezel/4ee11f6c05c48a36fe3d to your computer and use it in GitHub Desktop.
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 json | |
import time | |
import ujson | |
NUM_OBJ_KEYS = 1000 | |
NUM_RUNS = 1000000 | |
obj = {} | |
for i in range(NUM_OBJ_KEYS): | |
obj[i] = 'foo' | |
if __name__ == "__main__": | |
for json_module in (json, ujson): | |
serialized_obj = getattr(json_module, 'dumps')(obj) | |
times = [] | |
num_runs = NUM_RUNS | |
for operation in ('dumps', 'loads'): | |
print('# keys in object: {}. Runs: {}. json_module: {}. ' | |
'Operation: {}'.format(NUM_OBJ_KEYS, | |
NUM_RUNS, | |
json_module.__name__, | |
operation)) | |
for i in range(num_runs): | |
t1 = time.time() | |
if operation == 'loads': | |
operand = serialized_obj | |
else: | |
operand = obj | |
getattr(json_module, operation)(operand) | |
times.append(time.time() - t1) | |
avg = sum(times)/len(times) | |
print('average time:', avg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run on MB Pro Yosemite, 2.8 GHz Intel Core i7, 16GB RAM:
Dict with 10 keys, 1MM runs of each operation:
dumps: ujson took ~39% of the time of json
loads: ujson took ~38% of the time of json
Dict with 1000 keys, 1MM runs of each operation:
dumps: ujson took ~77% of the time of json
loads: ujson took 70% of the time of json