If you want some precise, "nice" estimates you can probably do all sorts of elaborate and fancy stuff. I only ever need to do this so that I can decide whether I should go get a snack while my program is running, or whether I should sit there and wait for it. So it's enough for me to get a very rough idea of whether the task will take seconds (wait), minutes (get a snack or coffee), hours (time for lunch!) or days (maybe I should optimize this a bit...).
Most of my time-intensive code tends to be a for loop with many iterations. So I just look at how many iterations there are, how many I've done, and how long it has taken me. There I have a key decision to make, it's either:
1) Time to complete an iteration is not significantly correlated to index of the iteration, in which case t_remaining = (t_elapsed / finished_iterations)*remaining_iterations
.
2) The index is related to the time per iteration (imagine sorting an array a by finding the maximum and appending it to an array b, removing that element from a, and repeating- finding the maximum will be quicker and quicker as you go on). In this case I would use (I've never needed to yet) t_remaining = t_last_iteration*remaining_iterations
.
I have a quick and dirty Matlab function that I call at the end of my for block with the loop's end condition and the current iteration as parameters. It prints information on what percentage of the loop is done and how much more time it will probably take (using method 1 above):
function Progress(now, final)
% Call at inside a time-intensive for-loop with the current i (or other
% counter) in now and the final bound of the loop in final. This function
% will decide whether the rounded percentage has increased, and if it has,
% print progress information to the console.
%
% This expects loops to start at 1 and go up. Transform your variables
% accordingly if that is not the case - if the first time you call this
% function, the first argument isn't 1, you're in trouble.
%
% The aim of this function is to provide feedback on the progress of the
% loop, while making sure exactly 100 lines are printed, regardless of the
% number of iterations in the loop.
if now == 1
tic;
end
t_elapsed = toc;
nowPercent = round(100 * now / final);
oldPercent = round(100 * (now - 1) / final);
% Calculation rate = (total iterations so far)/(total time passed so far)
if nowPercent > oldPercent
t_remaining = round((t_elapsed / now * (final - now)) / 6) / 10;
disp([num2str(nowPercent) '% complete, estimated ' ...
num2str(t_remaining) ' minutes remaining...']);
end
end
Matlab code is pretty self explanatory, in my opinion. The one slightly less-obvious thing: tic
starts a stopwatch, and toc
returns the time elapsed since the last tic
.
You would use it like so:
n = size(inputs);
for i = 1:n
do_complicated_calculation(inputs(i));
Progress(i, n);
end
It has many shortcomings, but they are obvious.