Reprinted from: http://blog.csdn.net/wzgbm/article/details/54691615
First give a simple example, the time to test the list and dict find:
ImportTime Query_lst = [-60000,-6000,- -,- -,-6,0,6, -, -,6000,60000] LST = [] dic = {} forIinchRange100000000): Lst.append (i) dic[i] =1Start = Time.time () forVinchQuery_lst:ifVinchLst:ContinueEnd1 = Time.time () forVinchQuery_lst:ifVinchDic:ContinueEnd2 = Time.time ()Print "List Search time:%f"% (End1-start)Print "Dict Search Time:%f"% (END2-END1)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
Operation Result:
List Search time:11.836798
Dict Search time:0.000007
We can see from the above example that the lookup efficiency of list is much lower than the efficiency of dict, for the following reasons:
The storage structure of a list object in Python uses a linear table, so its query complexity is O (n), while the storage structure of the Dict object uses a hash table, which in its best case is an O (1) query complexity.
Python find efficiency comparison in list and dict