Comparing execution times of search algorithms in a list I came up to a result that list.index() is VERY much faster than a simple for in. According to this they should be both O(n). I have this results in my tests:
The simple solution passes about 350 Tests within 3 seconds:
def linear_simple(arr):
for i in range(len(arr)):
if arr[i] == #my searched value#:
return i
The index solution passes all 2000 tests within 3 seconds (actually it is done even within 2 seconds):
def linear_index(arr):
return arr.index( #my searched value# )
All test arrays are randomly generated. The tests were made several times with similar results.
That means index() is about 9 times faster. Why? Is index() not simply iterating the same way over the list like for in?