[Numpy-discussion] slow numpy.clip ?

David Cournapeau david at ar.media.kyoto-u.ac.jp
Tue Dec 19 02:56:06 CST 2006


Eric Firing wrote:
> David Cournapeau wrote:
>> Well, this is something I would be willing to try *if* this is the main 
>> bottleneck of imshow/show. I am still unsure about the problem, because 
>> if I change numpy.clip to my function, including a copy, I really get a 
>> big difference myself:
>>
>> val = ma.array(nx.clip(val.filled(vmax), vmin, vmax),
>>                                 mask=mask)
>>
>> vs
>>
>> def myclip(b, m, M):
>>     a       = b.copy()
>>     a[a<m]  = m
>>     a[a>M]  = M
>>     return a
>> val = ma.array(myclip(val.filled(vmax), vmin, vmax), mask=mask)
>>
>> By trying the best result, I get 0.888 ms vs 0.784 for a show() call, 
>> which is already a 10 % improvement, and I get almost a 15 % if I remove 
>> the copy. I am updating numpy/scipy/mpl on my laptop to see if this is 
>> specific to the CPU of my workstation (big cache, high frequency clock, 
>> bi CPU with HT enabled).
>
> Please try the putmask version without the copy on your machines; I 
> expect it will be quite a bit faster on both machines.  The relative 
> speeds of the versions may differ widely depending on how many values 
> actually get changed, though.
On my workstation (dual xeon; I run each corresponding script 5 times 
and took the best result):
    - nx.clip takes ~ 170 ms (of 920 ms for the whole show call)
    - your fast clip, with copy: ~ 50 ms (of ~820 ms)
    - mine, with copy: ~50 ms (of ~830 ms)
    - your wo copy:  ~ 30 ms (of 830 ms)
    - mine wo copy:  ~ 40 ms (of 830 ms)

Same on my laptop (pentium M @ 1.2 Ghz):

    - nx.clip takes ~ 230 ms (of 1460 ms)
    - mine with copy ~ 70 ms (of 1200 ms)
    - mine wo copy ~ 55 ms (of 1300 ms)
    - yours with copy ~ 80 ms (of 1300 ms)
    - yours wo copy ~ 67 ms (of 1300 ms)

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.

cheers,

David



More information about the Numpy-discussion mailing list