#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
'''
Author:leo
Date&Time:2022/2/10 12:32
Project:python FileName:20210210_json不同库运行速度测试.py
Comment:
相同量级,测试三个常用json库的运行速度,结果:orjson速度最快,适合大型项目。
json_dumps:4.589947938919067
ujson_dumps:2.8540351390838623
orjson_dumps:0.3769984245300293
'''
import json
import ujson
import orjson
import time
def func_cost_time(func):
def inner(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
stop_time = time.time()
print(func.__name__ + ":" + str(stop_time - start_time))
return result
return inner
res_dict = {}
for i in range(1, 10000_0000):
res_dict[str(i)] = i
@func_cost_time
def json_dumps(obj):
return json.dumps(obj)
@func_cost_time
def ujson_dumps(obj):
return ujson.dumps(obj)
@func_cost_time
def orjson_dumps(obj):
return orjson.dumps(obj)
r1 = json_dumps(res_dict)
r2 = ujson_dumps(res_dict)
r3 = orjson_dumps(res_dict)