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
```

In [2]:

```
import random
```

In [3]:

```
positions = [(random.random(), random.random()) for i in xrange(10000000)]
```

In [4]:

```
%timeit closest((.5, .5), positions)
```

In [5]:

```
import numpy as np
```

In [6]:

```
positions = np.random.rand(10000000, 2)
```

In [7]:

```
positions.ndim, positions.shape
```

Out[7]:

In [8]:

```
positions.size
```

Out[8]:

In [9]:

```
x, y = positions[:,0], positions[:,1] # x and y contain the 1st and 2nd cols, respectively.
```

In [10]:

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

In [11]:

```
%timeit exec(In[10])
```