How to make this speed up

Cameron Laird claird at lairds.com
Tue Mar 30 18:51:33 EST 2004


In article <qnkptauymdq.fsf at arbutus.physics.mcmaster.ca>,
David M. Cooke <cookedm+news at physics.mcmaster.ca> wrote:
			.
			.
			.
>Use numarray or Numeric, and use arrays for your points. Also, do you
>need the distance, or will the square of the distance do? (that'll
>save you a sqrt).
>
>import numarray as NA
>
>def distance_between_points(plist):
>    # if plist was a list of N points in d dimensions, p is an array of
>size (N,d)
>    p = NA.asarray(plist)
>    N, d = p.shape
>    # dist2 is a (N,N) array of squared distance
>    dist2 = NA.zeros((N, N), type=p.type())
>    for i in range(d):
>        dist2 += NA.subtract.outer(p[:,i],p[:,i])**2
>    return NA.sqrt(dist2)
>
>This should be almost as fast as writing your own C extension (and
>much easier...)
			.
			.
			.
The "much easier" has me curious.  While I'm as insistent
as anyone about Python's universality, this is an area
where I see it with no advantage over C.  The simple-minded
C homologue requires no memory management or dereferencing,
at the source level, so it's as easy as C gets.  Are you
simply observing that, when working in Python, any require-
ment to introduce the machinery necessary for a second 
language makes the problem not easy?  Or is there another
comparison between the two that I haven't noticed?
-- 

Cameron Laird <claird at phaseit.net>
Business:  http://www.Phaseit.net



More information about the Python-list mailing list