MATLAB: Benefits of using datenum to convert a date to an equivalent number

benefitsdatenumfaster

Hi,
I have been handed a MATLAB code that I want to speed up. The function datenum has been used which takes a lot of time. Each file has an array of some 50000 dates and there might be hundreds of such files so it takes up a lot of time. I was wondering if I could skip that step altogether. I do not see a specific need to convert the dates, unless it is faster to pass as a parameter among functions or any such benefit. If I do not need to use the equivalent number in any way, are there any other benefits that one may get by converting dates to equivalent numbers?
Also if I deem it necessary, is there an equivalent function to datenum in C, so that I can use a mex file for the purpose?
Thank you in advance!
I appreciate your advice!

Best Answer

A) If the application never uses the serial date numbers as datenums then it would seem that one could, indeed simply dispense with the conversion. That should be easy enough to check. The only reason I could see for doing the conversion and not using the value would be if were trying to speedup some i/o or reduce the size of an external file--the datenum as a double could be written in a stream (unformatted) file and retrieved faster than the string format of the same date. Of course, that would entail using datestr on the other end for human consumption.
B) A precise duplication of Matlab datenum in C is highly unlikely to exist -- it has great flexibility and a specific reference value that pretty much makes it unique afaik. There are alternative formulations that could, in theory be used, with an offset for the origin. If dates are restricted to those in the relatively short time frame around current, any discrepancies between routines (barring an actual bug, of course) should be minimal. The one advantage of sticking w/ Matlab is that if one uses consistent generation of same then one can rely on comparisons as then rounding will be the same...that might break slightly with mixing two libraries.
Again, whether any of the above is a problem would depend mostly on what is actually being done in the application.
One thing to note would be whether the call to datenum uses the specific format string--that is quite a lot faster than having to go thru the logic to try to automagically parse the input string (assuming string input form, of course). See the doc's for datenum for more on this point.