estimate time needed for a code to finish

97 views (last 30 days)
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
Paulo Abelha
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--

Sign in to comment.

Accepted Answer

Alessandro Masullo
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
Naema
Naema on 5 Feb 2015
Edited: Naema on 5 Feb 2015
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

Sign in to comment.

More Answers (2)

John D'Errico
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.

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

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!