フィルターのクリア

how to speed up datestr conversion?

10 ビュー (過去 30 日間)
Fan Mei
Fan Mei 2017 年 5 月 5 日
コメント済み: dpb 2017 年 5 月 5 日
I want to know how to speed up the conversion from date string to date number. My code takes around 1 min to finish.
I have a large files with the first column of time strings, as below:
'17:11:17.02000'
'17:11:17.03000'
'17:11:17.04000'
'17:11:17.05000'
My conversion code:
tnum=datenum(M1.Var1)-datenum(2017,1,1)+datenum(2016,5,20);

回答 (3 件)

Guillaume
Guillaume 2017 年 5 月 5 日
編集済み: Guillaume 2017 年 5 月 5 日
Specifying the format would greatly speed up the conversion:
tnum = datenum(M1.Var1, 'HH.MM.SS.FFF') - datenum(2017,1,1) + datenum(2016,5,20); %only works this year!
However note that it will only keep the first three digits of the milliseconds. I don't know a way of telling datenum to accept 5 digits of millisecond. Note that even your original code did not keep the 5 digit correctly (e.g. .12345 gets rounded to .12339)
As fast as datenum conversion, more flexible and a lot more modern is to convert to datetime:
td = datetime(M1.Var, 'ConvertFrom', 'HH:mm:ss.SSSSS'); %keeps all digits of the millisecond correctly
[td.Year, td.Month, td.Day] = deal(2016, 5, 20); %Override date works regardless of the year it's executed in
td.Format = 'dd-MMM-uuuu HH:mm:ss.SSSSS'; %for display only. Set to what you prefer

Fan Mei
Fan Mei 2017 年 5 月 5 日
Thank you very much for your quick reply.

dpb
dpb 2017 年 5 月 5 日
Two alternatives--
dn=datenum(c,'HH:MM:SS.FFF')
if stick with outmoded datenum or switch to the newer datetime class...
dt=datetime(c,'inputformat','HH:mm:ss.SSSSS')
Either way, specify the specific input format string. You can't quite hit the precise string with datenum; it won't accept other than three-digit fractional seconds indicator. You could try
dn=datenum(c(:,1:12),'HH:MM:SS.FFF')
to see if the precise string length would make any improvement in speed (I'd guess probably not, but "ya' never knows".
I've never compared the two for speed so don't know if the newer class has more overhead owing to it being a class and all or whether there have been sufficient "tweaks" to make it perform as well or better than its predecessor. Would be interesting to know.
Oh...another possibility would be to do the conversion on input if you're using (or could use) textscan --
dt=textscan(fid,'%{HH:mm:ss.SSSSS}'D);
I dunno on performance with textscan, either, altho while it might be more overhead in the initial read, you might make up for it overall in eliminating the second specific conversion call. This will, of course, be a datetime array this way, so that is a difference in subsequent code.
  3 件のコメント
Fan Mei
Fan Mei 2017 年 5 月 5 日
Thank you very much
dpb
dpb 2017 年 5 月 5 日
If solved the problem, please Accept Answer to at least indicate closure of the topic if nothing else.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by