Why is this so slow?

2 ビュー (過去 30 日間)
Moe Szyslak
Moe Szyslak 2020 年 2 月 21 日
コメント済み: Moe Szyslak 2020 年 2 月 21 日
Hi, All -- the seemingly simple script shown below is exceedingly slow. It takes several minutes on a fast workstation. I can't figure out why it takes so long. What am I missing? Thanks, in advance.
N = 10^6;
a = 0;
b = 4;
r = a + (b-a)*rand(N,1);
c = r.*sinh(r);
k = (1:N)';
s(N,1) = 0;
for j = 1:N
s(j,1) = sum(c(1:j));
end
s = (b-a)*s./k;

採用された回答

madhan ravi
madhan ravi 2020 年 2 月 21 日
Instead of loop why not simply use cumsum() ?
  3 件のコメント
Rik
Rik 2020 年 2 月 21 日
If you want to implement it with a loop, you can still gain a lot of speed:
N=10^4;
c=rand(N,1);
tic
s=[];s(N,1)=0;
for n=1:N
s(n)=sum(c(1:n));
end
t1=toc;
tic
s=[];s(N,1)=0;
s(1)=c(1);
for n=2:N
s(n)=s(n-1)+c(n);
end
t2=toc;
tic
s=cumsum(c);
t3=toc;
clc
fprintf('fast loop is %.0f times faster\ncumsum is %.0f times faster\n',...
t1/t2,t1/t3)
That is over 100 times faster on my machine, with cumsum increasing the speed 10 times over that.
Moe Szyslak
Moe Szyslak 2020 年 2 月 21 日
That's much cleaner. I see similar speedups on my machine. Thanks again.

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

その他の回答 (1 件)

Rik
Rik 2020 年 2 月 21 日
Loops with many elements are slow. You also forgot to properly pre-allocate s.
N = 10^5;
a = 0;
b = 4;
r = a + (b-a)*rand(N,1);
c = r.*sinh(r);
k = (1:N)';
%s(N,1) = 0;
s=zeros(N,1);
for j = 1:N
s(j,1) = sum(c(1:j));
end
s = (b-a)*s./k;
clc
s2 = (b-a)*cumsum(c)./k;
fprintf('largest error = %.2e\n',max(abs(s-s2)))
  4 件のコメント
Rik
Rik 2020 年 2 月 21 日
It works if the variable doesn't exist yet, but if it already exists (e.g. because you aborted a 10^6 run to do a 10^5 instead), this will cause a dimension error with this code.
I didn't remember the conclusion from your question, so I looked it up.
%replace this
s(N,1) = 0;
%with this
s=[];s(N,1) = 0;
Because apparently that is faster.
Except in this case, where it is faster to create the array with cumsum instead.
madhan ravi
madhan ravi 2020 年 2 月 21 日
Yes Rik

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

カテゴリ

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

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by