Code covered by the BSD License  

Highlights from
duration

duration

by

 

08 Jul 2008 (Updated )

Calculate the duration of a distributed job or individual task

duration(obj)
function sec = duration(obj)
%DURATION Calculate the number of seconds from creation to finished.
%   SEC = DURATION(OBJ) calculate total time (in seconds) to run a
%   distributed job or task.
%
%   If no return argument is assigned, the duration is only displayed.  If
%   the job or task has not finished, use the current time to show duration
%   to this point.
%
%   DURATION only calculates time for tasks or jobs completed within 30
%   days.
%
%   Examples
%   ========
%   time = duration(job);
%   duration(job.Task(1))
%    515 seconds

%   Copyright 2007-2008 The MathWorks, Inc.
%   Raymond S. Norris (raymond.norris@mathworks.com)

% Get the create and finished times.  (Not sure if this is when create
% started or create finished).
ct = get(obj,'CreateTime');
ft = get(obj,'FinishTime');

if isempty(ft)
   if isempty(strfind(class(obj),'job'))==true
      typ = 'Task';
   else
      typ = 'Job';
   end
   disp([typ ' is still running.  Using current time as finished time.'])
   % datestr() doesn't support a time zone, so just put in EST.  It
   % doesn't matter for our calculations.
   ft = [datestr(now,'ddd mmm dd HH:MM:SS') ' EST ' datestr(now,'yyyy')];
end
if isempty(ct) || isempty(ft)
   sec = NaN;
   return
end

% In case there's an error somewhere, try/catch it and return NaN with no
% explanation.
try
   % Remove the standard time marking (e.g. EST) and the day (e.g. Thu).
   sidx = strfind(ct,' ');
   ct([1:4 sidx(4):sidx(5)-1]) = [];
   ft([1:4 sidx(4):sidx(5)-1]) = [];

   % Convert to a date number
   fmt = 'mmm dd HH:MM:SS yyyy';
   ct = datenum(ct,fmt);
   ft = datenum(ft,fmt);

   % Get time difference.  Let's assume that it doesn't go past 30 days.
   [y,mo,d,h,mi,s] = datevec(ft-ct);
   s = s + mi*60 + h*60*60 + d*24*60*60;

catch le
   disp(le.message)
   s = NaN;
end

if nargout==0
   if isnan(s), s=0; end
   disp([char(9) num2str(s) ' seconds'])
else
   sec = s;
end

Contact us