import math
def closest(position, positions):
x0, y0 = position
dbest, ibest = None, None
for i, (x,y) in enumerate(positions):
# compute the Euclidean distance
dist = ((x - x0) ** 2) + ((y - y0) ** 2)
dist = math.sqrt(dist)
if dbest is None or dist < dbest:
dbest, ibest = dist, i
return ibest
import random
positions = [(random.random(), random.random()) for i in range(10000000)]
for i in range(0, 10):
print(positions[i])
(0.8367067617471916, 0.8681954690987693) (0.24766284208655387, 0.1430469172954475) (0.37693240513198545, 0.9491498797122762) (0.7310683868163543, 0.8645436627528782) (0.8904739385229614, 0.1420507285593402) (0.8923964723761243, 0.6737510140986144) (0.6879572668201521, 0.2008072195186068) (0.14446217586642773, 0.8613472687511541) (0.71031134638013, 0.66847510504419) (0.8751207509656234, 0.8206094040619555)
%timeit p = closest((.5, .5), positions)
6.63 s ± 189 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
print( positions[closest((.5, .5), positions)] )
(0.49992528656550106, 0.4998538500083588)
import numpy as np
positions = np.array(positions)
positions.shape
(10000000, 2)
print(positions[:10,:])
[[0.83670676 0.86819547] [0.24766284 0.14304692] [0.37693241 0.94914988] [0.73106839 0.86454366] [0.89047394 0.14205073] [0.89239647 0.67375101] [0.68795727 0.20080722] [0.14446218 0.86134727] [0.71031135 0.66847511] [0.87512075 0.8206094 ]]
x, y = positions[:,0], positions[:,1] # x and y are arrays containing the 1st and 2nd cols, respectively.
distances = np.sqrt((x - 0.5)**2 + (y - 0.5)**2)
closest_dist = distances.min()
%%timeit
distances = np.sqrt( (x - 0.5)**2 + (y - 0.5)**2 )
226 ms ± 13 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
closest_dist = distances.min()
5.13 ms ± 58.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)