Reputation: 3019
I have a numpy array of roughly 3125000 entries the data is structured using the following dtype
dt = np.dtype([('startPoint', '<u8' ), ('endPoint', '<u8')])
The data is from a file that has been previously sorted by endPoint before it is read into the array.
I now need to search the array and check if it contains a particular endpoint and I'm doing this using a binary search using the following code
def binarySearch(array, index):
lowPoint = 0
highpoint = len(array) - 1
while (lowPoint <= highpoint):
midPoint = int((lowPoint + highpoint) / 2)
if(index == array[midPoint]['endPoint']):
return midPoint
elif(index < array[midPoint]['endPoint']):
highpoint = midPoint - 1
else:
lowPoint = midPoint + 1
return -1
My question is is there a faster way to search for an entry in this array. As in is there a built in Numpy search that may be faster than my binary search.
Upvotes: 3
Views: 11230
Reputation: 2158
Try numpy.searchsorted
, also you can use memory mapping if the array is too large. searchsorted is implemented as binary search.
Upvotes: 6