A very fast way to convert numbers into datetime ?
54 次查看(过去 30 天)
显示 更早的评论
Is there any faster way then what showed in the following example to convert numbers into datetime ?
% Input:
time = [ 2.02210270954498e+16
2.02210271650345e+16
2.02210270854021e+16
2.02210271304501e+16
2.02210271854288e+16
2.02210271748533e+16
2.02210271717544e+16
2.02210271659356e+16
2.02210271209301e+16
2.02210271145566e+16
2.0221027124415e+16
2.02210271043238e+16
2.02210271226421e+16
2.0221027194043e+16
2.02210271044584e+16
2.02210271747289e+16
2.02210271526449e+16
2.02210271400009e+16
2.02210271753212e+16
2.02210271008031e+16
2.02210271004171e+16
2.0221027114025e+16
2.02210270918585e+16
2.02210271549196e+16
2.0221027134255e+16
2.02210271520466e+16
2.02210271552524e+16
2.02210271056362e+16
2.02210271610381e+16
2.02210271157507e+16
2.02210271320401e+16
2.02210271334491e+16
2.02210271558493e+16
2.02210271811281e+16
2.02210271644034e+16
2.02210271209346e+16
2.02210271631079e+16
2.02210271045395e+16
2.02210271004233e+16
2.02210270945555e+16
2.02210271043468e+16
2.02210270929154e+16
2.02210271301133e+16
2.0221027175642e+16
2.02210271337459e+16
2.02210271644043e+16
2.02210271231201e+16
2.02210271624433e+16
2.02210271700406e+16
2.02210271803474e+16
2.02210271215051e+16
2.02210271624368e+16
2.02210271400011e+16
2.02210270820096e+16
2.02210271249262e+16
2.02210271303242e+16
2.02210270934301e+16
2.02210271023499e+16
2.02210271024505e+16
2.02210271234557e+16
2.0221027173109e+16
2.02210271141147e+16
2.0221027171211e+16
2.0221027171336e+16
2.02210271050369e+16
2.02210271100339e+16
2.02210271712135e+16
2.0221027093251e+16
2.02210270813426e+16
2.02210271536101e+16
2.02210272249422e+16
2.02210271139262e+16
2.02210271802585e+16
2.02210270932514e+16
2.0221027135005e+16
2.02210271647141e+16
2.02210271659521e+16
2.02210271152253e+16
2.02210271632355e+16
2.02210271106299e+16
2.02210271508318e+16
2.02210271013477e+16
2.02210271112198e+16
2.02210271126206e+16
2.02210271420079e+16
2.02210271702577e+16
2.02210271501366e+16
2.02210271045333e+16
2.02210271555391e+16
2.02210271514332e+16
2.02210271443004e+16
2.02210271010239e+16
2.022102712154e+16
2.02210271639551e+16
2.02210271321279e+16
2.02210271610501e+16
2.02210271320145e+16
2.02210271700372e+16
2.02210271023158e+16
2.02210271445046e+16];
T = table(time);
% Output:
% Any way to make the following part way any faster ?
tic
S = string(uint64(T.time));
T.time = datetime(S, 'inputFormat','uuuuMMddHHmmssSSS', 'Format','yyyy-MM-dd HH:mm:ss.SSS');
toc
0 个评论
采纳的回答
Eric Delgado
2022-12-9
编辑:Eric Delgado
2022-12-11
% Input:
time = [ 2.02210270954498e+16; ...
2.02210271650345e+16; ...
2.02210270854021e+16; ...
2.02210271304501e+16; ...
2.02210271854288e+16; ...
2.02210271748533e+16; ...
2.02210271717544e+16; ...
2.02210271659356e+16; ...
2.02210271209301e+16; ...
2.02210271145566e+16; ...
2.0221027124415e+16; ...
2.02210271043238e+16; ...
2.02210271226421e+16; ...
2.0221027194043e+16; ...
2.02210271044584e+16; ...
2.02210271747289e+16; ...
2.02210271526449e+16; ...
2.02210271445046e+16];
% Output:
% Any way to make the following part way any faster ?
S1 = string(uint64(time));
approach1_time = zeros(100,1);
approach2_time = zeros(100,1);
for ii = 1:100
tic
time1 = datetime(S1, 'inputFormat','uuuuMMddHHmmssSSS', 'Format','yyyy-MM-dd HH:mm:ss.SSS');
approach1_time(ii) = toc;
tic
time2 = datetime(S1, 'inputFormat','uuuuMMddHHmmssSSS');
time2.Format = 'yyyy-MM-dd HH:mm:ss.SSS';
approach2_time(ii) = toc;
end
mean(approach1_time)
mean(approach2_time)
4 个评论
Voss
2022-12-9
编辑:Voss
2022-12-9
@Sim: The purpose of my comment was to show that the time taken by the two different approaches for setting the Format of a datetime array are not significantly different. They appeared to be different in @Eric Delgado's answer, but that (I suspected - and the times in my comment seem to support my suspicion) was because his T2 was not contained in a table. In other words, I suspected that the time required to access a variable in a table explained most of the time difference. In my comment, I created a second table for what I thought would be a more appropriate test, which showed that the times are about the same. You definitely do not need to create a new table T2. I did that only for comparing the times of the two approaches.
Bottom line, it takes about the same amount of time whether you do it like this:
T1.time = datetime(S1, 'inputFormat','uuuuMMddHHmmssSSS', 'Format','yyyy-MM-dd HH:mm:ss.SSS');
Or like this:
T1.time = datetime(S1, 'inputFormat','uuuuMMddHHmmssSSS');
T1.time.Format = 'yyyy-MM-dd HH:mm:ss.SSS';
And in my opinion the first way is preferable for its concision.
(See the attached m-file for more reliable timing measurements using timeit instead of tic/toc.)
timing_test
更多回答(1 个)
Peter Perkins
2022-12-12
Syntactically,
>> time = datetime("202210270954498", 'inputFormat','uuuuMMddHHmmssSSS', 'Format','yyyy-MM-dd HH:mm:ss.SSS')
time =
datetime
2022-10-27 09:54:49.800
is easy to write (although if you really expect the last two S's in SSS to work, you are out of luck -- you need uint64, not double). But you did say "very fast". Performance-wise, you are converting numeric->string->datetime. There is
>> time = datetime(20221027,"ConvertFrom","yyyymmdd",'format','yyyy-MM-dd HH:mm:ss.SSS')
time =
datetime
2022-10-27 00:00:00.000
but no
time = datetime(20221027,"ConvertFrom","yyyyMMddHHmmssSSS",...
but it's not too hard to write the code: look at datetime/convertFrom and extend it. Likely to be faster for large data.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Array Geometries and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!