[Numpy-discussion] another performance problem
Travis Oliphant
oliphant.travis at ieee.org
Sun Jan 29 17:17:07 EST 2006
Gerard Vermeulen wrote:
>Hi Travis,
>
>max and min are really slow in numpy:
>
>
>
>>>>from timeit import Timer
>>>>import Numeric; Numeric.__version__
>>>>
>>>>
>'23.1'
>
>
>>>>import numarray; numarray.__version__
>>>>
>>>>
>'1.2.3'
>
>
>>>>import numpy; numpy.__version__
>>>>
>>>>
>'0.9.5.2021'
>
>
>>>>t1 = Timer('a = max(b)', 'import Numeric as N; b=N.cos(N.arange(0, 10**5, 1, N.Float))')
>>>>t2 = Timer('a = max(b)', 'import numarray as N; b=N.cos(N.arange(0, 10**5, 1, N.Float))')
>>>>t3 = Timer('a = max(b)', 'import numpy as N; b=N.cos(N.arange(0, 10**5, 1, N.Float))')
>>>>t1.timeit(10)
>>>>
>>>>
>0.13748002052307129
>
>
>>>>t2.timeit(10)
>>>>
>>>>
>0.64549708366394043
>
>
>>>>t3.timeit(10)
>>>>
>>>>
>4.5433549880981445
>
>
You shouldn't be using max(b)!!!
You should use b.max() because max is a Python function that is using
the generic sequence interface to do the comparisons. Very likely it is
slower because in numpy you get an array scalar (which don't yet have
their own math defined and so use the full ufunc machinery do to
computations).
At some point the array scalars will have their own math defined and
should be as fast as the Python scalars, so this particular "slowness"
will go away. But, you use maximum.reduce() instead anyway.
-Travis
More information about the NumPy-Discussion
mailing list