%% Using DateNum and DateVec in MATLAB 7.0.1
% Code that contains datenum or datevec may be much slower in MATLAB 7 than
% in MATLAB 6.5.

%% Example
% I use tic and toc to calculate the amount of time it takes to convert
% 1000 date strings (containing today's date) to serial date numbers.
% The results is that MATLAB 7.01 is more than 40% slower than MATLAB 6.5.1.

% In MATLAB 6.5.1:
tic, for k = 1:1000, d = datenum('25-Oct-2004'); end, toc

% In MATLAB 7.0.1:
tic, for k = 1:1000, d = datenum('25-Oct-2004'); end, toc

%% Solution
% The MATLAB 7 implementation of datenum and datevec does much more work to
% try to guess from a much longer list of supported formats and languages
% what the format of your date string is. As a result, the conversion
% between date strings and date numbers will take longer. However you can
% reduce the time taken by providing datenum or datevec with the optional
% date format string.
%
% The conversion of the 1000 date numbers now takes about 40% of the time
% it took in MATLAB 6.5. In addition, since we (South Africans) tend to use
% the dd/mm/yyyy format, this is easily achieved by specifying the format
% string:
%
% For those interested in a fast datevec like function, take a look at the
% function datestr2vec contributed to MATLAB Central by Per Isakson.

% In MATLAB 7.0.1:
tic, for k = 1:1000, d = datenum('25-Oct-2004','dd-mmm-yyyy'); end, toc
theDate = datenum('25/10/2004','dd/mm/yyyy')