python requests请求性能对比eval和json.loads

废话不多说,直接上代码!
分析如下:

json.loads是将baijson对象du转成原生对象
eval() 函数用来执行一个字符串表达式,并返回表达式的值。
json.loads()将json字符串转换为字典类型

举个例子数据100,看下运行效果

# coding:utf8

import sys, json
from time import time

a = {
    
    }


for i in range(0,100):
     a[i + 190000] = (24, 31020, 3.804021, 800569, 700052)

# 转成字符串
dict_str = repr(a)

t1 = time()

d = eval(dict_str)

t2 = time()
print("eval cost time is %f." % (t2 - t1))

# 转成json类型的字符串
dict_json = json.dumps(a)

t1 = time()

d = json.loads(dict_json)

t2 = time()

print("json cost time is %f." % (t2 - t1))

测试结果
在这里插入图片描述

运行1000条数据看下效果

# coding:utf8

import sys, json
from time import time

a = {
    
    }


for i in range(0,1000):
     a[i + 190000] = (24, 31020, 3.804021, 800569, 700052)

# 转成字符串
dict_str = repr(a)

t1 = time()

d = eval(dict_str)

t2 = time()
print("eval cost time is %f." % (t2 - t1))

# 转成json类型的字符串
dict_json = json.dumps(a)

t1 = time()

d = json.loads(dict_json)

t2 = time()

print("json cost time is %f." % (t2 - t1))

效果图
在这里插入图片描述
数据10000条时候,运行接口

# coding:utf8

import sys, json
from time import time

a = {
    
    }


for i in range(0,10000):
    a[i + 190000] = (24, 31020, 3.804021, 800569, 700052)

# 转成字符串
dict_str = repr(a)

t1 = time()

d = eval(dict_str)

t2 = time()
print("eval cost time is %f." % (t2 - t1))

# 转成json类型的字符串
dict_json = json.dumps(a)

t1 = time()

d = json.loads(dict_json)

t2 = time()

print("json cost time is %f." % (t2 - t1))

在这里插入图片描述

总结:eval性能上没有json.loads好。看个人需求大小了。

猜你喜欢

转载自blog.csdn.net/weixin_37254196/article/details/108019697