do disp iterations slow down the code?

46 ビュー (過去 30 日間)
Thales
Thales 2017 年 4 月 28 日
コメント済み: Thales 2017 年 4 月 28 日
If I have an iterative solver, i.e.,
while norm(P-Pold) > eps;
...
disp(sprintf('%10d %10.4f',k,norm(P-Pold)));
end
Does display the values of the variables slow down the code? I don't have to use the disp function, I could just not use the semicolon when calculating it
while normP > eps;
...
k = k+1
normP = norm(P-Pold)
end
The question just if printing every variable on the Command Window makes the code runs slower.
  1 件のコメント
Joseph Cheng
Joseph Cheng 2017 年 4 月 28 日
without appears to run sower
tic
for ind = 1:100
disp(sprintf('%f',ind))
end
spinup = toc;
tic
for ind = 1:100
disp(sprintf('%f',ind))
end
time1 = toc;
tic
for ind = 1:100
ind
end
time2= toc
tic
for ind = 1:100
disp(ind)
end
time3 = toc;
[spinup time1 time2 time3]
the above you can see which method takes longer. but i think the time should be consequential unless you're talking about a HUGE number of displays

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

採用された回答

John D'Errico
John D'Errico 2017 年 4 月 28 日
編集済み: John D'Errico 2017 年 4 月 28 日
Of course dumping stuff to the command window will slow things down. That forces MATLAB to format your variables for display. Plus, it forces MATLAB to scroll the command window. How would you not expect it to slow things down? Everything you do that takes away from actual computations will always slow the calculations down.
The point is, if you want fast code, then DON'T dump everything out.
At most, display information rarely, only what will be useful, perhaps to show that your code is working to your satisfaction.
Learn to use waitbars when that will help.
It is often useful to display information not every iteration, but perhaps every 100th iteration, etc.
Garrulous display is a killer of efficient code, even if the code is well written otherwise.
In my codes, I sometimes have used a multi-level display flag. Thus if I am debugging code, I want an in-depth display of information. But normal usage requires less information displayed. And when I know things are working perfectly, I allow all display to be turned off.
  1 件のコメント
Thales
Thales 2017 年 4 月 28 日
How much does it slow down? Is it worthy to display every hundred/thousand iterations?
if ~mod(k,100)
disp(sprintf('%10d %10.4f',k,norm(P-Pold)));
end
Do I save a lot of time doing it or do I waste just as much time because the mod and if operations? I want to accompany the code running to make sure it is working (not in an infinite loop or leading the answer to a wrong direction). Plus, the users usually prefer to see something on the screen to be sure the code is properly running. Can i do some sort of a progress bar or something? How do I know if the code is running or freezed, if I do not print anything?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by