Python requests request performance comparison eval and json.loads

Not much nonsense, just go to the code!
analyse as below:

json.loads converts the baijson object du into a native object
eval() function to execute a string expression and return the value of the expression.
json.loads() converts json string to dictionary type

For example, the data 100, see the running effect

# 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))

Test Results
Insert picture description here

Run 1000 pieces of data to see the effect

# 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))

Effect picture When the
Insert picture description here
data is 10000, the interface is running

# 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))

Insert picture description here

Summary: eval is not as good as json.loads in performance. It depends on personal needs.

Guess you like

Origin blog.csdn.net/weixin_37254196/article/details/108019697