Increase recursive loop performance
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Guilherme Roberto
am 4 Mär. 2016
Beantwortet: Walter Roberson
am 4 Mär. 2016
Hello guys, My code works with recursion, receiving an image as input, but it's taking a really long time to process it. I wonder if is there anything I can do to speed it up.
Here is the code:
ROTULO=0;
for i=xi:xf
for j=yi:yf
if(out(i,j,1)==-1)
ROTULO=ROTULO+1;
rotular(i,j);
end
end
end
And the function rotular:
function rotular(w,z)
out(w,z,1)=ROTULO;
if(w < xf && out(w+1,z,1)==-1)
rotular(w+1,z);
end
if(z < yf && out(w,z+1,1)==-1)
rotular(w,z+1);
end
if(w > xi && out(w-1,z,1)==-1)
rotular(w-1,z);
end
if(z > yi && out(w,z-1,1)==-1)
rotular(w,z-1);
end
end
The variable out is a matrix corresponding to the input image. I'm running MATLAB R2015b on Windows 8.1, 64-bit, 6.0GB RAM, Intel i5-3470S CPU @ 2.90GHz I'm not having any trouble concerning RAM. I am also not getting any errors. Just want to improve the performance.
Thanks.
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 4 Mär. 2016
In MATLAB, passing parameters explicitly is a little more efficient than shared variables, so it would be a little more efficient to pass in ROTULO and to return "out" from rotular
Shared variables are not so bad -- much more efficient than global -- but explicit variables are faster.
Also, R2015b apparently improved the efficiency of recursive routines.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!