示例代码:
import json
import time
s = '[{"name": "dgw", "age": 26, "sex": "男"}, {"name": "dgw2", "age": 27, "sex": "男"}, ' \
'{"name": "dgw3", "age": 28, "sex": "男"}]'
eval_start = time.time()
for _ in range(100000):
res = eval(s)
print(type(res), type(res[0]), res)
print(f"使用eval()用时:{time.time() - eval_start} s")
json_start = time.time()
for _ in range(100000):
res = json.loads(s)
print(type(res), type(res[0]), res)
print(f"使用json.loads用时:{time.time() - json_start} s")
运行结果:
总结:从运行结果来看,json.loads()的运行效率要远远高于eval()方法 。