A very fast way to convert numbers into datetime ?

4 Ansichten (letzte 30 Tage)
Sim
Sim am 8 Dez. 2022
Kommentiert: Sim am 14 Dez. 2022
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
Elapsed time is 0.023786 seconds.

Akzeptierte Antwort

Eric Delgado
Eric Delgado am 9 Dez. 2022
Bearbeitet: Eric Delgado am 11 Dez. 2022
You are right @Voss... I just run it 100 times and there's no difference at all. :(
% 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)
ans = 0.0016
mean(approach2_time)
ans = 0.0016
  4 Kommentare
Sim
Sim am 12 Dez. 2022
Thanks both @Eric Delgado and @Voss !
So, basically, if I understood correctly, the solution of @Eric Delgado is not so different from the solution I wrote in my question... So... @Eric Delgado, should I unaccept the answer since the time is more or less the same as mine ? Or should I just leave it as "accepted" and waiting for other potential answers ? :-)
Thank you very much for your efforts and time! :-)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Peter Perkins
Peter Perkins am 12 Dez. 2022
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.

Kategorien

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by