フィルターのクリア

Trying to reduce computation time

1 回表示 (過去 30 日間)
ThB
ThB 2017 年 1 月 25 日
編集済み: Jan 2017 年 1 月 25 日
Hey, I am trying to implement a code to compute the values of two functions using a large array of input.
visitst = symfun(exp(T/4)+1000,T);
visitsp = symfun(-10*(P-10)^2+5000,P);
for i=1:n
Values1(i) = visitst(Input1(i));
Values2(i) = visitsp(Input2(i));
end
I am looking to go through an "Input1" and "Input2" array of 100k+ different inputs, but even 1000 are taking extremely long computing times. Can someone suggest a method of improving this?

採用された回答

Jan
Jan 2017 年 1 月 25 日
Pre-allocation the output before the loop:
Value1 = zeros(1, n);
Value2 = zeros(1, n);
I do not expect, that this is the bottleneck. But the costs of a forgotton pre-allocation grow exponentially, such that there is a limit in the input size, where this becomes the bottleneck.
  2 件のコメント
ThB
ThB 2017 年 1 月 25 日
編集済み: ThB 2017 年 1 月 25 日
That just cut the computational time massively (from 30sec to 4sec for example). I just preallocated the size using
Value1 = zeros(n);
Thank you very much!
Any other ideas on how to speed things up?
Jan
Jan 2017 年 1 月 25 日
編集済み: Jan 2017 年 1 月 25 日
Wow, I'm surprised. Note that zeros(n) allocates a n*n matrix, but I'm not sure if the double class is sufficient for your case. Another method for an implicit pre-allocation is to run the loop backwards:
for i = n:-1:1 % Backwards for implicit pre-allocation
Then the last element is created at first, which reserves the complete vector at once - and in the matching class. Just be sure to add the comment, otherwise the readers (like you in 3 months) might wonder, what the purpose of this direction might be useful for.
For further speedups, use the profiler at first: Find the line, which uses the most processing time. Optimizing other parts is hardly useful.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeHistorical Contests についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by