Speeding up code by avoiding FOR, IF loops

13 visualizaciones (últimos 30 días)
Tim
Tim el 3 de Mzo. de 2012
Hi all, I have a question about how to speed up my code. I've been taught that for/if loops in matlab generally aren't great ideas, so I want to find a way to avoid them in the following code.
If you have suggestions on how to make these calculations outside of loops, I'd really appreciate it.
for i = 1:length(uniqueID)
for j = 1:length(timeList)
x = strmatch(uniqueID(i),idSeriesMonthly)
y = blsDataMonthly(x,[1 4])'
for k = 1:length(y)
if blsDataSeries(1,j) == y(1,k)
blsDataSeries(i + 1,j) = y(2,k)
end
end
end
end

Respuestas (1)

Jan
Jan el 3 de Mzo. de 2012
The rumor, that for-loops are slow in general concern the versions before 6.5 (July 2002). In modern Matlab versions the JIT can accelerate loops substantially.
In opposite to this strmatch is really slow. Better use strcmp or strncmp.
In addition the output to the command window consumes much time also. So append a semicolon after each assignment command.
You overwrite blsDataSeries(i + 1,j) repeatedly. So better start the innermost loop from the back and stop after the first match:
for k = length(y):-1:1
if blsDataSeries(1,j) == y(1,k)
blsDataSeries(i + 1,j) = y(2,k);
break; % Break out of the k-loop
end
end
Avoid the transposition of y.
[EDITED]
nTime = length(timeList);
blsDataSeries = zeros(length(uniqueID) + 1, nTime); % pre-allocate!
q = zeros(1, nTime);
for i = 1:length(uniqueID)
aID = uniqueID{i};
x = strncmp(aID, idSeriesMonthly, length(aID));
if sum(x) > 2
y1 = blsDataMonthly(x, 1);
y4 = blsDataMonthly(x, 4);
q(:) = NaN; % Re-use existing memory
for a = 1:length(y)
y1a = y1(a);
for b = nTime:-1:1
if y1a == timeList(b)
q(b) = y4(a);
break; % Break b-loop after the last occurrence
end
end
end
blsDataSeries(i + 1, :) = q;
end
end
  2 comentarios
Tim
Tim el 3 de Mzo. de 2012
Hey thanks for your help! Unfortunately this specifically didn't really speed up the process much. The code I'll post below did help, basically I ended up creating a new row to insert into the matrix rather than doing multiple for loops to locate specific cells.
It's still pretty slow but it's running at a much better clip (maybe 30mins for the whole thing, rather than multiple days). I think there's probably a way to vectorize the first FOR loop, but I'm not sure about it yet.
for i = 1:length(uniqueID);
x = strmatch(uniqueID(i),idSeriesMonthly);
y = blsDataMonthly(x,[1 4])';
if length(x) > 2; %there is one solo entry which screws with the q row variable later, but i don't need it
q = NaN(1,length(timeList));
for a = 1:length(y);
for b = 1:length(timeList);
if y(1,a) == timeList(b);
q(b) = y(2,a);
end
end
end
blsDataSeries(i + 1,:) = q;
end
end
Jan
Jan el 4 de Mzo. de 2012
Is blsDataSeries pre-allocated properly? If not, this array grows in each iteration, which wastes a lot of time. See [EDITED]. It would be easier to help, if the types and sizes of the used variables are explained.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by