how to speed up datestr conversion?

조회 수: 5 (최근 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 CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by