[Numpy-discussion] slow numpy.clip ?

Stefan van der Walt stefan at sun.ac.za
Mon Dec 18 02:27:56 CST 2006


Hi David

The benchmark below isn't quite correct.  In clip2_bench the data is
effectively only clipped once.  I attach a slightly modified version,
for which the benchmark results look like this:

   4 function calls in 4.631 CPU seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.003    0.003    4.631    4.631 clipb.py:10(bench_clip)
        1    2.149    2.149    2.149    2.149 clipb.py:16(clip1_bench)
        1    2.070    2.070    2.070    2.070 clipb.py:19(clip2_bench)
        1    0.409    0.409    0.409    0.409 clipb.py:6(generate_data_2d)
        0    0.000             0.000          profile:0(profiler)


The remaining difference is probably a cache effect.  If I change the
order, so that clip1_bench is executed last, I see:

   4 function calls in 5.250 CPU seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.003    0.003    5.250    5.250 clipb.py:10(bench_clip)
        1    2.588    2.588    2.588    2.588 clipb.py:19(clip2_bench)
        1    2.148    2.148    2.148    2.148 clipb.py:16(clip1_bench)
        1    0.512    0.512    0.512    0.512 clipb.py:6(generate_data_2d)
        0    0.000             0.000          profile:0(profiler)


Regards
Stéfan

On Mon, Dec 18, 2006 at 04:17:08PM +0900, David Cournapeau wrote:
> Hi,
> 
>     When trying to speed up some matplotlib routines with the matplotlib 
> dev team, I noticed that numpy.clip is pretty slow: clip(data, m, M) is 
> slower than a direct numpy implementation (that is data[data<m] = m; 
> data[data>M] = M; return data.copy()). My understanding is that the code 
> does the same thing, right ?
> 
>     Below, a small script which shows the difference (twice slower for a 
> 8000x256 array on my workstation):
> 

[...]
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clipb.py
Type: text/x-python
Size: 1029 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061218/d1de7208/attachment-0001.py 


More information about the Numpy-discussion mailing list