Comparison and Analysis of the Performance of Python judgment values in list or set; comparison of python Performance
This article compares and analyzes whether Python judges whether the value is executed in the list or set. We will share this with you for your reference. The details are as follows:
The speed of determining whether a value is in the set is much faster than that of the list, because the hash is used for searching the set, and the time is at the O (1) level.
Assume that listA has elements. setA = set (listA) is the set after setA is converted to listA.
The following is a simple comparison:
For I in xrange (0, 5000000): if I in listA: passfor I in xrange (0, 5000000): if I in setA: pass
The first cycle uses 16 min, and the second cycle uses 52 s. It can be seen that it is much more efficient to determine whether a value exists in set.
Besides, converting from list to set does not take any time.