Search code examples
pythonnumpyclip

Numpy clip function takes twice as long as expected


I'm finding that the performance of the numpy clip function is significantly slower than just doing it myself with a mask (164us vs about 74us). Is the clip function doing something additional that makes it take twice as long?

%timeit growth.clip(-maxg, maxg)
10000 loops, best of 3: 164 µs per loop

%timeit growth[np.greater(growth,maxg)] = maxg
10000 loops, best of 3: 37.1 µs per loop

%timeit growth[np.less(growth,-maxg)] = -maxg
10000 loops, best of 3: 37 µs per loop

After resetting the growth array and testing in the opposite order:

%timeit growth[np.less(growth,-maxg)] = -maxg
10000 loops, best of 3: 36.6 µs per loop

%timeit growth[np.greater(growth,maxg)] = maxg
10000 loops, best of 3: 37.3 µs per loop

%timeit growth.clip(-maxg, maxg)
100 loops, best of 3: 150 µs per loop

Note that growth is a fairly big array:

growth.shape
(12964, 7)

Solution

  • The default numpy.clip returns a new array with the clipped values. Using the argument out=growth makes the operation in-place:

    growth.clip(-maxg, maxg, out=growth)
    

    This way, the time taken by clip is more similar to the alternative that you mentioned.