[Numpy-discussion] Distance Matrix speed (original) (raw)
Tim Hochberg tim.hochberg at cox.net
Sun Jun 18 23🔞23 EDT 2006
- Previous message (by thread): [Numpy-discussion] Distance Matrix speed
- Next message (by thread): [Numpy-discussion] Distance Matrix speed
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 distloehner1(): 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
- Previous message (by thread): [Numpy-discussion] Distance Matrix speed
- Next message (by thread): [Numpy-discussion] Distance Matrix speed
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]