Benefits of using datenum to convert a date to an equivalent number?
Afficher commentaires plus anciens
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!
Réponse acceptée
Plus de réponses (1)
Matlab's date conversion functions are very smart and this requires a remarkable delay. If the input formats are exactly defined, a specific C-function can be very much faster. See e.g. FEX: DateStr2Num and FEX: DateConvert.
1 commentaire
I've never had so many to do that the time issue was an issue but I recall the comment before.
Wonder why, other than the front end, why once the format is set and all in the front end it should be so that the actual computation would be so comparatively slow? Seems like that should be straightforward after a little front-end overhead.
ADDENDUM:
Or, after reading the FEx comment, is it that DateNum does the string checking on every operation rather than just once on the first element (as I had presumed), perhaps?
ADDENDUM 2:
OK, datenum is doing it's utmost on every conversion; not always what one would hope but clearly why it's not a speed demon--
>> datestr(datenum(['12/31/2006';'31/12/2006']))
ans =
31-Dec-2006
12-Jul-2008
>> datestr(datenum(['31/12/2006';'12/31/2006';]))
ans =
28-May-0037
31-Dec-2006
>>
Creative if nothing else... :)
Catégories
En savoir plus sur Dates and Time dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!