1 Dictionaries and lists
The Python dictionary uses a hash table, so the complexity of the lookup operation is O (1), and the list is actually the array, in the list, the lookup needs to traverse the entire list, its complexity is O (n), so the operator dictionary of lookup access to members is faster than list.
From time Import time
T = time ()
list = [' A ', ' B ', ' is ', ' python ', ' Jason ', ' Hello ', ' hill ', ' with ', ' phone ', ' test ',
' Dfdf ', ' apple ', ' PDDF ', ' ind ', ' basic ', ' none ', ' baecr ', ' var ', ' Bana ', ' dd ', ' WRD ']
#list = Dict.fromkeys (list,true)
Print List
Filter = []
For I in Range (1000000):
For find in [' was ', ' hat ', ' new ', ' list ', ' old ', '. ']:
If find not in list:
Filter.append (Find)
Print "Total run time:"
Print time ()-T
The above code will probably need 16.09seconds to run. If the line #list = Dict.fromkeys (list,true) Comment is removed, the list is converted to a dictionary and then run, and the time is approximately 8.375 seconds, which is about half the efficiency increase. Therefore, using dict instead of list is a good choice when multiple data members are needed for frequent lookups or visits.
2 Sets and lists (list)
The union,intersection,difference operation of set is faster than the iteration of the list, so if a list intersection is involved, the set, or the bad problem can be converted to set to operate
Finding the intersection of lists
From time Import time
T = time ()
LISTA=[1,2,3,4,5,6,7,8,9,13,34,53,42,44]
LISTB=[2,4,6,9,23]
Intersection=[]
For I in Range (1000000):
For a in Lista:
For B in Listb:
If a = = B:
Intersection.append (a)
Print "Total run time:"
Print time ()-T
The operating time of the above program is roughly:
Total run Time:
38.4070000648
Optimized code Listing:
From time Import time
T = time ()
LISTA=[1,2,3,4,5,6,7,8,9,13,34,53,42,44]
LISTB=[2,4,6,9,23]
Intersection=[]
For I in Range (1000000):
List (set (Lista) &set (LISTB))
Print "Total run time:"
Print time ()-T
The running time of the program is reduced to 8.75, and the running time is greatly shortened after the set is changed to 4 times times.
Table 1. Common usage of Set
syntax |
action |
|
|
union |
|
|
intersection |
|
Set (List1) –set (LIST2) |
Difference |
The collection of elements appearing in List1 but not appearing in List2 |
Python Performance Optimization Tips