#### Let's consider the problem of finding the closest restaurant to our current GPS coordinates. Let's assume the current position is given as an (x,y) coordinate, and that we have coordinates of various restaurants stored in a list of positions.¶

In :
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


#### First we'll create a random list of coordinates. To make it realistic, let's create 10 M coordinates.¶

In :
import random

In :
positions = [(random.random(), random.random()) for i in range(10000000)]

In :
for i in range(0, 10):
print(positions[i])

(0.34897171546698946, 0.48777739791707764)
(0.5974786286055563, 0.5419029996255541)
(0.955086771121624, 0.5872136797125914)
(0.027190030811679144, 0.1707567022589218)
(0.8824067440891363, 0.8385162040290873)
(0.6817065736751843, 0.5720734803968918)
(0.110856105460713, 0.17722746493728392)
(0.09156325023291645, 0.2585030392438351)
(0.1351192445316184, 0.5177234271825397)
(0.009325670218637283, 0.30884677804264793)


#### Let's see how long it takes to compute the closest distance to our current coordinates: (0.5,0.5)¶

In :
%timeit closest((.5, .5), positions)

7.47 s ± 138 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)


#### Now let's try doing something similar with Numpy. Numpy arrays are much more efficient, and so is the method for random number generation.¶

In :
import numpy as np

In :
positions = np.random.rand(10000000, 2)

In :
positions.shape

Out:
(10000000, 2)
In :
print(positions[:10,:])

[[0.79528408 0.34554301]
[0.94386009 0.17115728]
[0.69632382 0.86300158]
[0.04312253 0.11622507]
[0.32317689 0.76841256]
[0.62116145 0.53263306]
[0.85947551 0.45664064]
[0.25950421 0.35563115]
[0.0534494  0.69352409]
[0.73937439 0.91306357]]


#### Now let's again compute the distances to our position (0.5, 0.5)¶

In :
x, y = positions[:,0], positions[:,1] # x and y are arrays containing the 1st and 2nd cols, respectively.

In :
distances = np.sqrt( (x - 0.5)**2 + (y - 0.5)**2 )

In :
print(distances[:10])

[0.33324112 0.55240321 0.41269019 0.59667429 0.32142139 0.12547913
0.36208103 0.28050061 0.48668164 0.47741136]


#### Let's see how much faster was Numpy compared to standard Python¶

In :
%timeit exec{In}

269 ms ± 4.14 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [ ]: