[Numpy-discussion] Distance Matrix speed
Christopher Barker
Chris.Barker at noaa.gov
Fri Jun 16 13:05:33 EDT 2006
Bruce Southey wrote:
> Please run the exact same code in Matlab that you are running in
> NumPy. Many of Matlab functions are very highly optimized so these are
> provided as binary functions. I think that you are running into this
> so you are not doing the correct comparison
He is doing the correct comparison: if Matlab has some built-in compiled
utility functions that numpy doesn't -- it really is faster!
It looks like other's suggestions show that well written numpy code is
plenty fast, however.
One more suggestion I don't think I've seen: numpy provides a built-in
compiled utility function: hypot()
>>> x = N.arange(5)
>>> y = N.arange(5)
>>> N.hypot(x,y)
array([ 0. , 1.41421356, 2.82842712, 4.24264069, 5.65685425])
>>> N.sqrt(x**2 + y**2)
array([ 0. , 1.41421356, 2.82842712, 4.24264069, 5.65685425])
Timings:
>>> timeit.Timer('N.sqrt(x**2 + y**2)','import numpy as N; x =
N.arange(5000); y = N.arange(5000)').timeit(100)
0.49785208702087402
>>> timeit.Timer('N.hypot(x,y)','import numpy as N; x = N.arange(5000);
y = N.arange(5000)').timeit(100)
0.081479072570800781
A factor of 6 improvement.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the NumPy-Discussion
mailing list