[Numpy-discussion] Distance Matrix speed

Tim Hochberg tim.hochberg at cox.net
Sun Jun 18 23:18:23 EDT 2006


Alan G Isaac wrote:

>On Sun, 18 Jun 2006, Sebastian Beca apparently wrote: 
>  
>
>>def dist():
>>d = zeros([N, C], dtype=float)
>>if N < C: for i in range(N):
>> xy = A[i] - B d[i,:] = sqrt(sum(xy**2, axis=1))
>> return d
>>else:
>> for j in range(C):
>> xy = A - B[j] d[:,j] = sqrt(sum(xy**2, axis=1))
>>return d 
>>    
>>
>
>
>But that is 50% slower than Johannes's version:
>
>def dist_loehner1():
>        d = A[:, newaxis, :] - B[newaxis, :, :]
>        d = sqrt((d**2).sum(axis=2))
>	return d
>  
>
Are you sure about that? I just ran it through timeit, using Sebastian's 
array sizes and I get Sebastian's version being 150% *faster*. This 
could well be cache size dependant, so may vary from box to box, but I'd 
expect Sebastian's current version to scale better in general.


-tim







More information about the NumPy-Discussion mailing list