Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster function than datenum in MATLAB

Does anybody knows a faster way to convert date string (2010-12-12 12:21:12.123) to number?

like image 849
OHLÁLÁ Avatar asked Apr 28 '11 12:04

OHLÁLÁ


3 Answers

It is often instructional to profile the built-in Matlab functions and extract just the internal functionality of interest.

In your particular case,

dtstr2dtnummx({'2010-12-12 12:21:12.123'},'yyyy-MM-dd HH:mm:ss')

is 3 times faster (takes 30% of the time) than:

datenum({'2010-12-12 12:21:12.123'},'yyyy-mm-dd HH:MM:SS')

where dtstr2dtnummx is an internal function (C:\Program Files\Matlab\R2011a\toolbox\matlab\timefun\private\dtstr2dtnummx.mexw32 on my Windows machine).

To gain access to this internal function, simply add its folder to the Matlab path using the addpath function, or copy the dtstr2dtnummx.mexw32 file to another folder that is already on your Matlab path.

Note that the string format is different between dtstr2dtnummx and datenum, so be careful!

To those interested, the folder above contains other interesting date conversion functions, so explore and enjoy!

Note 5/5/2011: I have now posted an article that expands this answer on http://undocumentedmatlab.com/blog/datenum-performance/

like image 69
Yair Altman Avatar answered Oct 05 '22 07:10

Yair Altman


Often you need to take a systems approach. I had a very similar problem, when I was extracting thousands of dates from a DB. It turns out that many modern DBs (Postgres, Sql server & Oracle are the ones that I tried) all can do the conversion from their date representations to the Matlab date representations several order of magnitudes quicker than the text to datenum on the matlab side. If this data is coming from a DB, think DB-side conversion!!

like image 31
Marc Avatar answered Oct 05 '22 07:10

Marc


Presumably if you care about the time spent converting dates, you're converting many of them. Even JIT optimizations in recent versions of matlab aside, you'll get much faster results calling

datenum(cellarrayofdates, 'yyyy-mm-dd HH:MM:SS');

than

for i=1:length(cellarrayofdates); datenum(cellarrayofdates{i}, 'yyyy-mm-dd HH:MM:SS'); end

If you're not already doing that, start there, since it allows matlab to reduce the overhead of figuring out your date format each for each call to the function.

like image 32
arcticmac Avatar answered Oct 05 '22 06:10

arcticmac