# How can I speed up my for Loop in matlab?

7 views (last 30 days)
Harsh Rob on 22 Dec 2019
Commented: Walter Roberson on 23 Dec 2019
I am running this code in which the code runs very slow. I have already run my code for more than 48 hours and still did not get the output, thus had to stop the code.
Can someone help me speed up this code ? It took me about 6 min to run the for loop just for one day.
For loop is taking the maximum amount of time in this code. Also, I need to run the entire code for interval 3 min, 6 min, 9 min, 12 min, 15 min, 18 min and 21 min.
The value of n changes to 1498 days. I have done the testing only for one day in my code. Can we also write a loop which does the entire calculation and plot the graph of interval vs rv (interval is x axis vs rv is y axis).
interval = 3;
n=1;
%3:3:21; %Number of minutes for the sampling frequency
%Total number of days between 1Dec 2014 to 7th Jan 2019 is equal to 1498 days
slotu = (n*(24*(60/interval)))-1; %Number of slots per overall data as per the sampling frequency
returnsForDay = []; %Create a blank matrix of returns of all days which gets filled onebyone
previousReturn = 0; %Initialised value as 0 but keeps changing everytime
rv =[]; %Matrix to store values of realised variance per day
counter = 0;
while startDate <= datetime("1-Dec-2014 23:59:59") %Define a condition where the last date is defined
%the for loop function in this code - This is just for one day calculation of RV
tic
for i = 0:slotu
startDate = startDate + minutes(00:interval:interval);
endDate = startDate + minutes(00:01:01)
temp1 = datestr(startDate,'dd-mmm-yyyy HH:MM:SS');
temp2 = datestr(endDate,'dd-mmm-yyyy HH:MM:SS');
test12 = ALLDataVector.data(ALLDataVector.test >= temp1(2,:) & ALLDataVector.test < temp2(2,:))
if(isempty(test12))
tempreturn = ALLDataVector.data(ALLDataVector.test <= temp1(2,:));
%tempreturn2 = ALLDataVector.data(ALLDataVector.test >= temp1(2,:));
if(isempty(tempreturn))
previousValue = 300;
else
previousValue = tempreturn(end);
end
currentValue = previousValue;
else
currentValue = test12(1,:);
end
newcol = length(returnsForDay);
returnsForDay(newcol + 1) = currentValue - previousReturn;
previousReturn = currentValue;
end %for loop is closed
toc
% alpha =0.999; %Constant introduced in the function
[RV]=realzvariation(returnsForDay); %Function call
rv(counter + 1) = RV;
end %While loop is closed
%This is for reference
% FUnction call code - [RV]
function [RV]=realzvariation(r)
m= length(r);
RV=sum(r.^2);

Image Analyst on 22 Dec 2019
I ran the attached code in 4.1 seconds.
Done with 479 iterations in 4.143622 seconds
It's about 1 second per 100 rows, so a million rows would take 8649 seconds (144 minutes) - quite some time.
You're tacking on one point to the end of returnsForDay and that definitely takes time. Maybe you could preallocate some huge amount of space for it and just use indexing to poke in the right locations.
Maybe using the parallel processing toolbox could help - you could ask the Mathworks or here on how to use it (I'm not very familiar with it).
Harsh Rob on 23 Dec 2019
Thanks mate
Walter Roberson on 23 Dec 2019
See also your question in which you talk about a version of this converted for parfor. The techniques I discuss there can be used here as well. And be sure to pre-allocate the arrays.