[Numpy-discussion] array.min() vs. min(array)
Travis Oliphant
oliphant.travis at ieee.org
Wed Apr 26 14:42:05 EDT 2006
Ryan Krauss wrote:
> I was spending some time trying to track down how to speed up an
> algorithm that gets called a bunch of times during an optimization. I
> was startled when I finally figured out that most of the time was
> wasted by using the built-in pyhton min function. It turns out that
> in my case, using array.min() (i.e. the method of the Numpy array) is
> 300-500 times faster than the built-in python min function (i.e.
> min(array)).
>
> So, thank you Travis and everyone who has put so much time into
> thinking through Numpy and making it fast (as well as making sure it
> is correct).
The builtin min function is a bit confusing because it usually does work
on NumPy arrays. But, as you've noticed it is always slower because it
uses the "generic sequence interface" that NumPy arrays expose. So,
it's basically not much faster than a Python loop. In this case you are
also being hit by the fact that scalarmath is not yet implemented (it's
getting close though...) so the returned array scalars are being
compared using the bulky ufunc machinery on each element separately.
In Python 2.5 we are going to have the same issues with the new any()
and all() functions of Python.
-Travis
More information about the NumPy-Discussion
mailing list