In [1]:
def closest(position, positions):
x0, y0 = position
dbest, ibest = None, None
for i, (x, y) in enumerate(positions):
d = (x - x0) ** 2 + (y - y0) ** 2
if dbest is None or d < dbest:
dbest, ibest = d, i
return ibest
import random
We generate a list of random positions.
In [2]:
positions = [(random.random(), random.random()) for _ in xrange(10000000)]
Now we evaluate the time required to compute the closest position from (0.5, 0.5)
.
In [3]:
%timeit closest((.5, .5), positions)
First, we need to activate the pylab mode so that NumPy is loaded and the NumPy objects are available in the current namespace.
In [4]:
%pylab
Generating random positions with NumPy's rand
function is much faster than using a list.
In [5]:
positions = rand(10000000, 2)
positions
is a NumPy ndarray
object.
In [6]:
type(positions)
Out[6]:
It has two dimensions and a shape of (10000000, 2)
.
In [7]:
positions.ndim, positions.shape
Out[7]:
We can easily get the columns of this matrix, which contain here the x and y coordinates of all positions.
In [8]:
x, y = positions[:,0], positions[:,1]
Now we compute the distances in a vectorized fashion, which is much more efficient than with a Python loop.
In [9]:
distances = (x - .5) ** 2 + (y - .5) ** 2
In [10]:
%timeit exec In[9]
In [11]:
%timeit ibest = distances.argmin()
Using NumPy is about 35 times faster than using pure Python code here.