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 [1]:
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 [2]:
import random
In [3]:
positions = [(random.random(), random.random()) for i in range(10000000)]
In [4]:
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 [5]:
%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 [6]:
import numpy as np
In [7]:
positions = np.random.rand(10000000, 2)
In [8]:
positions.shape
Out[8]:
(10000000, 2)
In [9]:
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 [10]:
x, y = positions[:,0], positions[:,1] # x and y are arrays containing the 1st and 2nd cols, respectively.
In [11]:
distances = np.sqrt( (x - 0.5)**2 + (y - 0.5)**2 )
In [12]:
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 [13]:
%timeit exec{In[11]}
269 ms ± 4.14 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [ ]: