[Numpy-discussion] Need help for implementing a fast clip in numpy (was slow clip)

A. M. Archibald peridot.faceted at gmail.com
Wed Jan 10 16:39:23 CST 2007


On 10/01/07, Stefan van der Walt <stefan at sun.ac.za> wrote:
> On Wed, Jan 10, 2007 at 08:28:14PM +0100, Francesc Altet wrote:
> > El dt 09 de 01 del 2007 a les 23:19 +0900, en/na David Cournapeau va
> > escriure:
> > > Hi,
> > >
> > >     I am finally implementing a C function to replace the current slow
> > > implementation of clip in python as promised a few weeks ago. The idea
> > > is to implement it as the following:
> > >
> > > def clip(input, min, max):
> > >     a   = input.copy()
> > >     putmask(a, a <= min, min)
> > >     putmask(a, a >= max, max)
> > >     return a

> > It is interesting to see there how fancy-indexing + assignation is quite
> > more efficient than putmask.
>
> Not on my machine:

It is almost certain that the relative efficiency of the different
numpy methods depends on what fraction of the data actually needs
clipping, so different test data (or real data) is likely to favour
one over the other.

Why not write the algorithm in C? None of the above is relevant for C,
where the algorithm should be "traverse the array; for each element,
clip it". If speed is a big concern, the inner loop could be
customized for each data type, and max() and min() should probably be
used. But in any case, it will almost certainly run faster than any of
the above solutions. And distributing the C should pose no problems -
just toss it in with the zillion other C functions in numpy.

Perverse as it sounds, f2py might actually set things up rather
tidily, leaving a FORTRAN function of a handful of lines; even the
data type switching could be done in python.

A. M. Archibald


More information about the Numpy-discussion mailing list