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):
>
[...]
import numpy as N
#==========================
# To benchmark imshow alone
#==========================
def generate_data_2d(fr, nwin, hop, len):
nframes = 1.0 * fr / hop * len
return N.random.randn(nframes, nwin)
def bench_clip():
m = -1.
M = 1.
# 2 minutes (120 sec) of sounds @ 8 kHz with 256 samples with 50 % overlap
data = generate_data_2d(8000, 256, 128, 120)
def clip1_bench(data, niter):
for i in range(niter):
blop = data.clip(m, M)
def clip2_bench(data, niter):
for i in range(niter):
blop = data.copy()
blop[blop<m] = m
blop[blop>M] = M
clip2_bench(data, 10)
clip1_bench(data, 10)
if __name__ == '__main__':
# test clip
import hotshot, hotshot.stats
profile_file = 'clip.prof'
prof = hotshot.Profile(profile_file, lineevents=1)
prof.runcall(bench_clip)
p = hotshot.stats.load(profile_file)
print p.sort_stats('cumulative').print_stats(20)
prof.close()
_______________________________________________
Numpy-discussion mailing list
[email protected]
http://projects.scipy.org/mailman/listinfo/numpy-discussion