Why are DATETIME operations so slow?
14 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2018 年 5 月 17 日
編集済み: MathWorks Support Team
2024 年 9 月 13 日
I am performing many arithmetic operations on datetime values, but the operations take a while. How can I speed this up?
採用された回答
MathWorks Support Team
2024 年 9 月 13 日
編集済み: MathWorks Support Team
2024 年 9 月 13 日
In general, if you are performing many arithmetic operations one-by-one, you might want to see if there is any way you can vectorize your code. MATLAB is optimized for vectorization, so if you are able to generate two vectors of datetimes to subtract, subtracting those two vectors will generally be faster than subtracting each element individually.
Please refer to the following for more information about vectorization:
Like many other array types in MATLAB, datetime arrays are designed for vectorized operations. It's possible to work on them by writing element-wise loops that access or assign to one element at a time. But datetime arrays encourage writing vectorized code because the operations that they support are already vectorized. As a simple example, imagine wanting to count the number of elements of a datetime array d1 that fall in the second half of the year. You could write a loop
count = 0;
for i = 1:length(d1)
if d1(i) > '30-Jun-2018'
count = count + 1;
end
end
But it's much simpler and faster to do the equivalent vectorized calculation.
count = sum(d1 > '30-Jun-2018')
As a slightly more involved example, imagine wanting to add 1 hour each element of a datetime array d1, but only on Fridays. You could write a loop to do that for each element,
for i = 1:length(d1)
if day(d1(i),'dayofweek') == 6
d1(i) = d1(i) + hours(1);
end
end
but again there is a simple equivalent vectorized calculation. Vectorized code is often easier to read and easier to maintain than element-wise loops.
d1 = d1 + (day(d1,'dayofweek') == 6)*hours(1);
Some operations are sequential, where the operation on one element depends on the previous elements. It may seem this kind of operation requires looping over each element in the array, but you can often avoid explicit loops by using functions such as diff, dateshift
, isweekend, operators such as > or ==, and properties such as .Hour
as building blocks for a vectorized implementation. And in some cases, functions such as movmean, which computes a moving windowed mean, provide exactly the calculation that's needed.
Still, some sequential operations cannot be vectorized, and some non-sequential operations involve a computation on each element that is so complicated that it is not convenient to vectorize it. In those cases, it may be necessary to do the calculation by looping over each element.
Under the hood, element-wise subscripting to access or assign to elements of a datetime array is more complex than the analogous subscripting on, for example, a numeric array. Therefore, in some cases, the performance of an element-wise loop over a datetime array will not be acceptable. Typically, these cases involve simple operations on each element. In those cases, it's possible to make a lossless conversion to a numeric type before the loop, use that in the loop body, and then convert back. This assumes that the element-wise operations are simple enough to be done with a raw numeric value.
One choice would be to use a Posix time representation:
p1 = posixtime(d1);
for i = 1:length(p1)
p2(i) = ...
end
d2 = datetime(p2,'convertFrom','posixtime');
However, your first option for performance should always be to try to vectorize your code.
2 件のコメント
Walter Roberson
2018 年 5 月 22 日
Note that you will lose precision when you do this. Serial date numbers have a resolution of roughly 10 microseconds. If your datetime objects have nanoseconds then conversion to serial date numbers will lose that information.
Walter Roberson
2021 年 9 月 30 日
I see that the response was edited.
posix time representation is lower precision than datetime is.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!