How to make this speed up

David M. Cooke cookedm+news at
Wed Mar 31 02:36:19 CEST 2004

At some point, claird at (Cameron Laird) wrote:

> In article <qnkptauymdq.fsf at>,
> David M. Cooke <cookedm+news at> 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?

Well, let me qualify: much easier for me, because
1) I have numarray installed, and am not worried about depending upon
2) I know how to use it efficiently
3) I don't have to look up the sequence API. For a C extension, I
presume if you're not using arrays (and lists instead), you'd be
mucking around with pulling things out of lists, and creating a new
one. I can easily imagine that code (with error checks, etc.) getting
to be a few hundred lines. I haven't tried, but Pyrex probably won't
be too much faster either, as it still has to index the lists.

A C extension written to use numarray (or Numeric) arrays will be
shorter and easier to write than the equivalent using lists. I'd first
evaluate whether the procedure I wrote above is "good enough", though.

|David M. Cooke

More information about the Python-list mailing list