[Numpy-discussion] slow numpy.clip ?
robert.kern at gmail.com
Tue Dec 19 03:37:51 CST 2006
David Cournapeau wrote:
> Basically, at least from those figures, both versions are pretty
> similar, and not worth improving much anyway for matplotlib. There is
> something funny with numpy version, though.
Looking at the code, it's certainly not surprising that the current
implementation of clip() is slow. It is a direct numpy C API translation of the
following (taken from numarray, but it is the same in Numeric):
def clip(m, m_min, m_max):
"""clip() returns a new array with every entry in m that is less than m_min
replaced by m_min, and every entry greater than m_max replaced by m_max.
selector = ufunc.less(m, m_min)+2*ufunc.greater(m, m_max)
return choose(selector, (m, m_min, m_max))
Creating that integer selector array is probably the most expensive part.
Copying the array, then using putmask() or similar is certainly a better
approach, and I can see no drawbacks to it.
If anyone is up to translating their faster clip() into C, I'm more than happy
to check it in. I might also entertain adding a copy=True keyword argument, but
I'm not entirely certain we should be expanding the API during the 1.0.x series.
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion