[Numpy-discussion] Faster

Keith Goodman kwgoodman@gmail....
Fri May 2 22:48:27 CDT 2008


On Fri, May 2, 2008 at 7:25 PM, Robert Kern <robert.kern@gmail.com> wrote:
>  In [5]: def kerndist(x):
>    ...:     N = x.shape[0]
>    ...:     x.flat[::N+1] = x.max()
>    ...:     ij = argmin(x.flat)
>    ...:     i, j = divmod(ij, N)
>    ...:     return i, j

I replaced

ij = argmin(x.flat)

with

x.argmin()

(they're the same in this context, right?) for a slight speed up. Now
I'm down to 1.9 seconds.

>> timeit argmin(x.flat)
10000 loops, best of 3: 24.8 µs per loop
>> timeit argmin(x)
100000 loops, best of 3: 9.19 µs per loop
>> timeit x.argmin()
100000 loops, best of 3: 8.06 µs per loop

OK, enough way-too-early optimization. But I'll definately add i, j =
modiv(x.argmin(), x.shape[0]) to my toolbelt.


More information about the Numpy-discussion mailing list