More multiplication operations require less time

16 ビュー (過去 30 日間)
Matt J
Matt J 2023 年 8 月 29 日
コメント済み: Matt J 2023 年 8 月 31 日
I would expect the execution times for the 3 operations below to get longer and longer. Where have I misled myself? Is it an issue with tic/toc as the timing method, or something else?
A=rand(500,500,500);
tic;
A.*A;
toc;
Elapsed time is 0.256989 seconds.
tic;
A.*A.*A;
toc;
Elapsed time is 0.124557 seconds.
tic;
A.*A.*A.*A.*A.*A;
toc;
Elapsed time is 0.099304 seconds.

採用された回答

Walter Roberson
Walter Roberson 2023 年 8 月 29 日
It is because you are not recording the output.
I introduced T0 here because I was noticing that in my tests, T1 (the first operation) was consistently slower than T2 (the second operation), and I suspected that time to parse or something similar was being allocated against the first operation. With the T0 introduced, the measured time for A.*A reduces.
A=rand(500,500,500);
tic;
T0 = A;
toc;
Elapsed time is 0.001954 seconds.
tic;
T1 = A.*A;
toc;
Elapsed time is 0.199039 seconds.
tic;
T2 = A.*A.*A;
toc;
Elapsed time is 0.201588 seconds.
tic;
T3 = A.*A.*A.*A.*A.*A;
toc;
Elapsed time is 0.208999 seconds.
  8 件のコメント
Walter Roberson
Walter Roberson 2023 年 8 月 30 日
Oh, right, it makes sense for James to have done that work! (But it would also have made sense for John to have done it as part of his high precision packages.)
Matt J
Matt J 2023 年 8 月 31 日
Part of the reason that the cost doesn't grow linearly here is that several .* calls in one operation are optimized so that the results are computed in one go, without intermediate arrays being constructed.
I see. Well, that seems like a very well-intentioned optimization, but hazardous for users trying to compare algorithms. It makes it impossible for the user to know if the operations they code are implemented literally.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by