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好。看个人需求大小了。

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值