estimate time needed for a code to finish

64 次查看(过去 30 天)
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 个评论
Star Strider
Star Strider 2015-2-2
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
Paulo Abelha 2016-9-17
I've coded a function that might help you: https://uk.mathworks.com/matlabcentral/fileexchange/59187-displayestimatedtimeofloop--tot-toc--curr-ix--tot-iter--

请先登录,再进行评论。

采纳的回答

Alessandro Masullo
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 个评论
Alessandro Masullo
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;
Naema
Naema 2015-2-5
编辑:Naema 2015-2-5
thanks, Alessandro, it is so good to use tic.In fact, all the answers to this question seem to be correct and helpful. Thank you all

请先登录,再进行评论。

更多回答(2 个)

John D'Errico
John D'Errico 2015-2-2
编辑:John D'Errico 2015-2-2
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.

Harsh Karve
Harsh Karve 2015-2-2
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;

类别

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