Find the nearest value in the list

Hi,

I’d like to find an index of the biggest value in the array that smaller than my provided number.
In this array, I will have more than 2 values.
Example:

val = 0.25
arr = [0, 0.1, 0.2, 0.4, 0.42, 0.5, 0.56, 1]
result = my_func(arr, val)

So the first smaller value is 0.2 and

result = 2

Right now I’m doing it with python implementation of binary search. But I think there is some faster approach for PyTorch.
Because it’s a huge bottleneck for me.
Maybe it’s similar to MaxPool implementation, but I don’t really know.

Thank you,
Tim