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;
댓글 수: 0
채택된 답변
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
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!