Re: For Loop minimisation
- From: Peter Boettcher <boettcher@xxxxxxxxxx>
- Date: Tue, 19 Feb 2008 10:24:30 -0500
sbm <SBM.inc@xxxxxxxxx> writes:
Hello
I wrote code to approximate the slow background variations using ASF.
I used the "for" loop....but unfortunately my it takes around 43
seconds to execute that loop.. whereas my whole program takes only 49
seconds.(ie other codes takes only 6 seconds)..
for s = 1:18
se = strel('disk',s*3);
iopen = imopen(I_fg,se);
iclose = imclose(iopen,se);
I_fg = iclose;
end
is there anyway to do this without "for" loop or any ways to minimise
the execution time.
The only reason to eliminate "for" loops is to reduce the overhead of
the loop code itself, and (more importantly) to eliminate the function
call overhead. In this case, with such a small set of iterations, the
loop overhead and function call overhead is basically zero compared to
the computation time of the function.
Does that make sense? In "old-school MATLAB", adding N scalars via a
loop would cost:
N*(overhead of MATLAB plus function) + N*(cost of CPU + operation)
The CPU + operation is about 1 CPU cycle (~ 1ns), while the MATLAB
function overhead was probably at least 100ns. So the function call
overhead dominated, which meant the "vectorized" version was worth it,
by eliminating N-1 function calls. (All this has changed with the
Just-in-time compiler and the accelerator).
In your case the fact the function call to strel still costs (for the
sake of argument) 100ns, but the computation is many seconds. So don't
bother trying to vectorize. The cost is all inside.
-Peter
.
- References:
- For Loop minimisation
- From: sbm
- For Loop minimisation
- Prev by Date: HELP varying color in QUIVER and COMPASS
- Next by Date: GUIDE "edit" objects clip text in Leopard
- Previous by thread: Re: For Loop minimisation
- Next by thread: string manipulation delimiter
- Index(es):
Relevant Pages
|