[Numpy-discussion] timing results (was: record arrays initialization)

Keith Goodman kwgoodman at gmail.com
Thu May 3 18:49:22 EDT 2012


On Thu, May 3, 2012 at 3:12 PM, Moroney, Catherine M (388D)
<Catherine.M.Moroney at jpl.nasa.gov> wrote:

> Here is the python code:
>
> def single(element, targets):
>
>    if (isinstance(element, tuple)):
>        xelement = element[0]
>    elif (isinstance(element, numpy.ndarray)):
>        xelement = element
>    else:
>        return FILL
>
>    nlen = xelement.shape[0]
>    nvec = targets.data.shape[0]
>    x = xelement.reshape(1, nlen).repeat(nvec, axis=0)

repeat is slow. I don't think you need it since broadcasting should
take care of things. (But maybe I misunderstand the data.)

>    diffs = ((x - targets.data)**2).sum(axis=1)

You could try np.dot instead of sum: one = np.ones(7); diff =
np.dot(diff, one). You could even pass one in.

>    diffs = numpy.sqrt(diffs)

Since you don't return the distance, no need for the sqrt. If you do
need the sqrt only take the sqrt of one element instead of all
elements.

>    return int(numpy.argmin(diffs, axis=0))



More information about the NumPy-Discussion mailing list