More multiplication operations require less time
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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
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 件のコメント
Matt J
2023 年 8 月 29 日
That is interesting, but I still wonder why there isn't a linear increase in execution time with the number of operations.
More or less linear if the A's are different. I guess a^n is easier to evaluate than a1*a2*...*an.
for i = 1:6
A{i}=rand(500,500,500);
end
tic
M2 = A{1}.*A{2};
t(2) = toc;
tic
M3 = A{1}.*A{2}.*A{3};
t(3) = toc;
tic
M4 = A{1}.*A{2}.*A{3}.*A{4};
t(4) = toc;
tic
M5 = A{1}.*A{2}.*A{3}.*A{4}.*A{5};
t(5) = toc;
tic
M6 = A{1}.*A{2}.*A{3}.*A{4}.*A{5}.*A{6};
t(6) = toc;
plot(2:6,t(2:6))

Matt J
2023 年 8 月 30 日
I guess a^n is easier to evaluate than a1*a2*...*an.
Hard to see why that would be true.
Walter Roberson
2023 年 8 月 30 日
I remember that several years ago, someone posted the results of timing tests showing the time differences between:
- naive repeated multiplication
- .^ operation
- realpow()
- log(), multiply, exp()
- decomposition of integer powers -- e.g., x.^12 -> temp1 = x.^3, temp2 = temp1.^2, result = temp2.^2. I seem to recall that someone (@John D'Errico maybe?) posted a FEX contribution to do this kind of decomposition
... though thinking back I am not sure that the time for the log approach was measured.
Christine Tobler
2023 年 8 月 30 日
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.
For the operation here, I'd say most of the time isn't spent in the multiplication, but in accessing the memory of each of the input arrays, and writing this into the output arrays (cache effects).
So when we have the same array A 6 times, only the elements of that one array are being traversed and loaded from RAM to the CPU. Doing 5 multiplications instead of just 1 isn't much more expensive if the relevant numbers are all already available in the CPU registers, compared to the cost of getting the data to the CPU in the first place.
For 6 different arrays A, the corresponding element of each array now has to be loaded into the CPU at the same time, which is more expensive.
Bruno Luong
2023 年 8 月 30 日
編集済み: Bruno Luong
2023 年 8 月 30 日
I just recently test .^ with base scalar and integer power and it seems MATLAB does NOT use power2 decomposition, but straighfoward successive multiplications.
IIRC James Tursa have submited something on power-2 decomposition.
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
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 件)
カテゴリ
ヘルプ センター および File Exchange で MATLAB についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
