Created
December 12, 2024 15:47
-
-
Save paulwinex/ab0e45d79079f80a431b7ad55f717856 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 os | |
import time | |
import orjson | |
import ujson | |
file = 'data.json' | |
with open(file, 'r') as f: | |
file_data = f.read() | |
data = json.loads(file_data) | |
count = 50000 | |
# json | |
start = time.perf_counter() | |
for i in range(count): | |
json.loads(file_data) | |
end = time.perf_counter() | |
json_time_decode = end - start | |
start = time.perf_counter() | |
for i in range(count): | |
json.dumps(data) | |
end = time.perf_counter() | |
json_time_encode = end - start | |
# ujson | |
start = time.perf_counter() | |
for i in range(count): | |
ujson.loads(file_data) | |
end = time.perf_counter() | |
ujson_time_decode = end - start | |
start = time.perf_counter() | |
for i in range(count): | |
ujson.dumps(data) | |
end = time.perf_counter() | |
ujson_time_encode = end - start | |
# orjson | |
start = time.perf_counter() | |
for i in range(count): | |
orjson.loads(file_data) | |
end = time.perf_counter() | |
orjson_time_decode = end - start | |
start = time.perf_counter() | |
for i in range(count): | |
orjson.dumps(data) | |
end = time.perf_counter() | |
orjson_time_encode = end - start | |
print(f'Data size: {int(os.path.getsize(file)/1024)}kb') | |
print(f'Operation: loads() x {count}') | |
print(f'Json time: {round(json_time_decode, 2)}s/{round(json_time_encode)}s') | |
print(f'Orjson time: {round(orjson_time_decode, 2)}s [{round(json_time_decode/orjson_time_decode, 2)}x speed] / {round(orjson_time_encode, 2)}s [{round(json_time_encode/orjson_time_encode, 2)}x speed]') | |
print(f'Ujson time: {round(ujson_time_decode, 2)}s [{round(json_time_decode/ujson_time_decode, 2)}x speed] / {round(ujson_time_encode, 2)}s [{round(json_time_encode/ujson_time_encode, 2)}x speed]') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment