简体   繁体   中英

Is there a faster way to search a numpy array

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.

Try numpy.searchsorted , also you can use memory mapping if the array is too large. searchsorted is implemented as binary search.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM