Short function taking 20+ minutes to run
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Cary
el 21 de Jul. de 2015
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 comentarios
Respuesta aceptada
the cyclist
el 21 de Jul. de 2015
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 comentarios
Muthu Annamalai
el 21 de Jul. de 2015
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
el 21 de Jul. de 2015
Editada: Cedric
el 21 de Jul. de 2015
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).
Más respuestas (0)
Ver también
Categorías
Más información sobre Whos en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!