Re: For Loop minimisation



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
.



Relevant Pages

  • Re: JVM vs my VM
    ... The only overhead compared to new allocation within the loop is the allocation itself - the initialization would need to be done in either case. ... Just because the possibility for this optimization exists, and HotSpot is documented to do this sort of thing, doesn't mean that it will happen for a given scenario. ...
    (comp.lang.java.programmer)
  • Re: RfD: rev 1 of TRAVERSE-WORDLIST proposal
    ... that transfers the state either to the data stack or memory is fairly ... If we want to use a generic word like NR>, we incur the overhead ... other than a single loop (a nested loop for hash tables with external ... to manually turn the control structure into data, ...
    (comp.lang.forth)
  • Re: Managed vs Unmanaged Bare Bones Performance Test
    ... just the basic stuff inside the CLR i.e. function calling ... both call a function passing an int by value from inside a for loop. ... //record the overhead for calling the performance counter API ...
    (microsoft.public.dotnet.framework)
  • Re: JVM vs my VM
    ... The only overhead compared to new allocation within the loop is the allocation itself - the initialization would need to be done in either case. ... If the preallocated object's value does not change, then of course you would create a final reference to it outside the loop, but if you have to load new values each time through then you do that regardless. ... GCs don't traverse dead objects. ...
    (comp.lang.java.programmer)
  • Re: Assembly string functions in i386 libc
    ... of string functions on i386? ... for the usual overhead for string instructions). ... The cmpb in this takes 2 cycles, but the loop overhead takes no more ... versions using the string instructions are much ...
    (freebsd-arch)