I did an experiment in which I tried to find the time needed to find a python list. I have a list of arr with random integers. arr_s contains only those items that have been sorted.
arr = np.random.randint(low = 0, high = 1000, size = 500) arr_s = sorted(arr)
Now I create a random array of find integers that has the elements I want to look for in arr and arr_s .
>>> %%timeit ...:find = np.random.randint(0, 1000, 600) ...:for i in find: ...: if i in arr: ...: continue [OUT]:100 loops, best of 3: 2.18 ms per loop >>> %%timeit ...:find = np.random.randint(0, 1000, 600) ...:for i in find: ...: if i in arr_s: ...: continue [OUT]:100 loops, best of 3: 5.15 ms per loop
Now I understand that I did not use any specific method to search in a sorted array (e.g. binary search). Thus, it can be a standard linear search, but why does it take much longer to search in a sorted array, which is in an unsorted array? I think it will take about the same time. I tried all kinds of find arrays. Arrays that have integers from (0, 1000), (-1000, -100) and (-10000, 10000) for a sorted array always take more cycles.
source share