eval Follow json.loads
It 's a different function , There are different functions , But in some places, the functions of the two functions are the same , At this time, if there is a certain requirement for the execution efficiency , Not recommended eval, change to the use of sth. json.loads There will be surprises .

<> One ,eval

eval() Function to execute a string expression , And returns the value of the expression .

<> Two ,json.loads

json.loads For decoding JSON data . This function returns Python Data type of field .

<> Three , Efficiency comparison

eval Follow json.loads
Sometimes it is used to convert data stored in list string or dictionary string to python object , Make it python Of list,dict Data in format , Easy to calculate .
This test is only for the above comparison
def test_time_list(func): from time import time start = time() a = str(list(
range(10000))) for i in range(100): func(a) print('{} Time: {:5.2f}s'.format(str
(func),time() - start)) def test_time_dict(func): from time import time a_dict =
{} for i in range(10000): a_dict[i]=i start = time() a = str(json.dumps(a_dict))
for i in range(100): func(a) print('{} Time: {:5.2f}s'.format(str(func),time() -
start)) if __name__ == '__main__': import json,msgpack test_time_list(eval)
test_time_list(json.loads) test_time_dict(eval) test_time_dict(json.loads)
The output is :

You can see , use json.loads The results are better than eval It's going to be fast 10 More than times , Faster list conversion .

But use json.loads There's a place to pay attention to ,json.loads There are no requirements for the list , But there are certain requirements for the format of the dictionary ,key Value to be kept in double quotes key, Single quoted key perhaps int All types will report errors .
The content of error reporting is as follows
json.decoder.JSONDecodeError: Expecting property name enclosed in double
quotes: line 1 column 2 (char 1)

Therefore, the dictionary suggests that the storage format should be json.dumps Convert to standard json Format is then stored as a string .

In addition, if it is in other serialization format , Yes json If you are not satisfied with the speed, you can consider using it msgpack, It's easy to use , But it also has certain requirements for the format , A lot of bloggers have tested it , It's all about json Several times faster . I don't know .

I am a moving ant , Hope to move forward together .

If it helps you a little , One like is enough , thank !

notes : If there are any mistakes and suggestions in this blog , Welcome to point out , esteem it a favor !!!

Technology
©2019-2020 Toolsou All rights reserved,
Python Basic knowledge and notes 2021 year 1 Monthly programmer salary statistics , average 14915 element GDOI2019 travels C++ Standard library What should I do if I suddenly encounter a question I can't answer during the interview ?2021 year 2 Monthly programmer salary statistics , average 15144 element college examination for the self-taught An overview of Marxism use C++ I want to talk to you “ Prototype mode ” ( copy / copy constructor ) How to use it quickly html and css Write static page Bitcoin in ten years ,VDS Opportunity or fraud