5

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)
tim654321
  • 2,218
  • 2
  • 15
  • 19

1 Answers1

11

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.

YS-L
  • 14,358
  • 3
  • 47
  • 58