[Numpy-discussion] speed of numpy.ndarray compared to Numeric.array

Sebastian Berg sebastian at sipsolutions.net
Mon Jan 10 06:22:50 EST 2011


Hey,

On Mon, 2011-01-10 at 08:09 +0000, EMMEL Thomas wrote:
> #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> def bruteForceSearch(points, point):
> 
>     minpt = min([(vec2Norm(pt, point), pt, i)
>                  for i, pt in enumerate(points)], key=itemgetter(0))
>     return sqrt(minpt[0]), minpt[1], minpt[2] 
>     
> #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> def vec2Norm(pt1,pt2):
>     xDis = pt1[0]-pt2[0]
>     yDis = pt1[1]-pt2[1]
>     zDis = pt1[2]-pt2[2]
>     return xDis*xDis+yDis*yDis+zDis*zDis
> 
> I have a more clever method but it still takes a lot of time in the vec2norm-function.
> If you like I can attach a running example.
> 

if you use the vec2Norm function as you wrote it there, this code is not
vectorized at all, and as such of course numpy would be slowest as it
has the most overhead and no advantages for non vectorized code, you
simply can't write python code like that and expect it to be fast for
these kind of calculations.

Your function should look more like this:

import numpy as np

def bruteForceSearch(points, point):
    dists = points - point
    # that may need point[None,:] or such for broadcasting to work
    dists *= dists
    dists = dists.sum(1)
    I = np.argmin(dists)
    return sqrt(dists[I]), points[I], I

If points is small, this may not help much (though compared to this
exact code my guess is it probably would), if points is larger it should
speed up things tremendously (unless you run into RAM problems). It may
be that you need to fiddle around with axes, I did not check the code.
If this is not good enough for you (you will need to port it (and maybe
the next outer loop as well) to Cython or write it in C/C++ and make
sure it can optimize things right. Also I think somewhere in scipy there
were some distance tools that may be already in C and nice fast, but not
sure.

I hope I got this right and it helps,

Sebastian




More information about the NumPy-Discussion mailing list