Short function taking 20+ minutes to run

2 次查看(过去 30 天)
I'm using a Xeon quad-core processor with 64GB of RAM. The program running the function only has 89 data points. It's now been over 20 minutes and MATLAB is still "busy" computing the program. Does my code below reveal any reason why it's taking so long to compute?
function last15MinsOfDay=last15MinsOfDay(time,price)
% last15MinsOfDay takes the average of prices between 3:45 and 4:00.
timeStr=cellstr(datestr(time));
timeDbl=datevec(timeStr);
times=and(timeDbl(:,4)==14,timeDbl(:,5)>=45)+and(timeDbl(:,4)==15,timeDbl(:,5)==0);
priceIdx=find(times);
z=find(fwdshift(1,priceIdx)~=priceIdx+1);
z=[1; z];
mu=zeros(length(z),1);
for i = 1:length(z);
while i < length(z)
mu(i)=mean(price(priceIdx(z(i):z(i+1))));
end
end
last15MinsOfDay=mu;

采纳的回答

the cyclist
the cyclist 2015-7-21
When your code hits this line:
while i < length(z)
the value of i is 1, and the length of z is whatever it is. The condition will always be true, because i and z do not change. Therefore, you will never break out of that while loop.
Not sure what you intended, but the while loop seems completely unnecessary here.
  3 个评论
Muthu Annamalai
Muthu Annamalai 2015-7-21
Nice catch @cyclist !
@Cary : If you use a profiler like,
profile on
call_function_with_arguments %last15MinsofDay
profile off
profile info
then you may find out where you are spending the most time.
Cedric
Cedric 2015-7-21
编辑:Cedric 2015-7-21
Also use the debugger to see what is happening in your code. Press F12 in the editor with the cursor on the line where you want to set a break point (or click on the little dash on the left), then press F5 for running your code (it will stop at the break point), and step manually by pressing F10 or F11 for step or "step in" respectively. Note that at any time you can observe the content/state of variables (in the workspace, by typing variable names in the command window, or by passing the cursor over variables in the editor).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by