estimate time needed for a code to finish
39 views (last 30 days)
Show older comments
Hi: I am not sure if my problem can be solved. I ran a code in matlab approximately two weeks ago. I have ran a similar one before and it took around 4 or 5 days and I was able to get the result but this current code is still processing! I wish I have included a function or something that could track the processing and let me know how much time is left. I do not like to stop the code, is there any way I could know how much is left or when the code will finish processing?
thanks
Naema
2 Comments
Star Strider
on 2 Feb 2015
There is probably no automatic way to do what you want. When I was running code that took several hours (back when both computers and MATLAB were slow), I used clock to create a start time for my code, then at each iteration, I printed out to the Command Window the iteration number, the maximum number of iterations I had requested (it used a for loop, updating parameters and solving a model-reference control problem), the elapsed time for the entire routine to that point, and the elapsed time for the previous iteration. I then used those data for it to estimate a time to finish, that it also reported at each iteration.
If you’re doing such long calculations, I also encourage you to write all your interim data to a .mat file, so you don’t lose everything if something goes wrong. Consider writing your code so you can continue from where it stopped if such a tragedy occurs, rather than having to start over from the beginning.
Paulo Abelha
on 17 Sep 2016
I've coded a function that might help you: https://uk.mathworks.com/matlabcentral/fileexchange/59187-displayestimatedtimeofloop--tot-toc--curr-ix--tot-iter--
Accepted Answer
Alessandro Masullo
on 3 Feb 2015
This is my favourite solution:
tic
for i = 1:1e9
if toc > 10
fprintf('Iteration %d/%d\n',i,1e9)
tic
end
% Very hard calculation
end
3 Comments
Alessandro Masullo
on 4 Feb 2015
Sure:
clear all;
close all;
clc;
x=-2500:2:2500-2;
y=-2500:2:2500-2;
[X,Y]=meshgrid(x,y);
rf=1125;
m= (-2500:2:2500);
n= (-2500:2:2500);
%Reading the data
Eyspp=dlmread('50t10w2500xy.txt');
C=zeros(length(X),length(Y));
a=1;
tic
for xx=-2500:1:2500;
if toc > 10
fprintf('xx %d\n',xx)
tic
end
b=1;
for yy=-2500:1:2500;
Eyfib=exp(-((X-xx).^2+(Y-yy).^2)./rf^2);
fn_int=Eyfib.*conj(Eyspp);
Norma = sum(sum(Eyspp.*conj(Eyspp)))*sum(sum(Eyfib.*conj(Eyfib)));
C(a,b)=sum(sum(fn_int)./sqrt(Norma));
b=b+1;
end
a=a+1;
end
figure(1);
imagesc(m,n,abs(C));title('overlap');xlabel('nm')
axis square;
grid on;
More Answers (2)
John D'Errico
on 2 Feb 2015
Edited: John D'Errico
on 2 Feb 2015
A good practice for long running codes is a progressbar. You can use the waitbar tool in matlab, or there are MANY such tools on the FEX, with various capabilites. Just look for one that has good reviews. Personally, I liked this one by Steve Hoelzer, but there are many such tools to be found.
Such a tool sits inside the main loop of your code, where you call it every time through the loop. It will update a small waitbar gui on your screen as it runs, showing the % progress though the loop. There are also tools that will create feedback directly on the command line.
One problem with these tools is if your loop will run millions of times through. Then the progressbar tool will actually add a significant amount of time to your code. That difference can be significant. In such cases, I'll put a test inside the loop, only calling the progressbar code every 100th or perhaps every 10000th time through the loop. This is as simple as doing a test on the loop counter, something like
if (mod(loopcounter,10000) == 0,
(update the progressbar here)
end
As an even simpler alternative, you can just dump information to the command line as you progress through the code, whatever information would be informative to you.
Of course, you cannot interrogate a currently running process. So you would need to add these codes to your script or function in advance.
Finally, a problem with any progress measure is if you do not know how many times it will need to go through the loop, how can you know when it will be done? So the above schemes only apply where there is some simple numeric measure of progress available. So for example, an optimization might run arbitrarily long, as you have no idea how many iterations will be needed. Even there though, you can still use the 'display' options on a tool like fmincon. This gives you useful information to help you know if it is actually making progress or not.
Whenever I have a very long running code, I may use some form of output to the command line, that will allow me to restart the process at a point near where it was, in case I must break into the process. Even better is to periodically write out a file that saves the current state of a process, in a way that I can restart things in the event of a problem. The nice thing about writing out a text file is you can view the file separately to see how things are progressing.
0 Comments
Harsh Karve
on 2 Feb 2015
If your code is a large loop, you can run one or two iterations and profile them. The profiler will give you a good idea of which parts of the code are taking longer as well as the execution time of each line.
Example:
profile on;
% some code here
profile off;
profile viewer;
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!