% FUNCTION CHRONO = CHRONO_CHECK(CHRONO, ITER)
%
% Print out for the user (how much time left).
%
% In:
%   chrono: object created by CHRONO_START
%   iter: integer, current iteration (from 1 to chrono.n)
%
% Out: updated chrono object.
 
function chrono = chrono_check(chrono, iter)

if nargin < 2
  error('Need at least 2 parameters');
end

current_time = 1e5 * now;
elapsed_time = current_time - chrono.start_time;

chrono.n = max(iter, chrono.n);

if current_time - chrono.previous_time > chrono.interval
  
  % Estimate average speed & time left
  speed = iter / elapsed_time;
  time_left = (chrono.n - iter) / speed;
  
  % Print out
  s = ['Iter %d/%d - Speed: %.3f iter per sec /' ...
       ' Time elapsed: %.0f sec, left: %.0f sec'];
  disp(sprintf(s, iter, chrono.n, speed, elapsed_time, time_left));
  
  % Update chrono object
  chrono.previous_time = current_time;
  
end
