Does keyboard slows down the code?

4 ビュー (過去 30 日間)
Alessandro Masullo
Alessandro Masullo 2015 年 4 月 17 日
コメント済み: Adam 2015 年 4 月 17 日
Hello.
I was doing some tests to speed up my code, and I realized that having a keyboard in the code slows it down, even if the keyboard is actually never hit.
Here's a code to check it:
% speed test
a = rand;
tic
for i = 1:1e4
if a == 0
keyboard
end
end
toc
tic
for i = 1:1e4
if a == 0
b = 5^2;
end
end
toc
Elapsed time is 0.000195 seconds.
Elapsed time is 0.000025 seconds.
How can this be possible?
  1 件のコメント
Adam
Adam 2015 年 4 月 17 日
Why would you expect it to make no difference. I don't know how keyboard input is handled by Matlab, but there is obviously an object of some sort created by the 'keyboard' command that gets ready to receive keyboard input. Creation of this object is unlikely to come for free and the Matlab code doesn't know that the user is not going to type anything.

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

採用された回答

Robert Cumming
Robert Cumming 2015 年 4 月 17 日
Its to do with the JIT optimisation, the same is true if you insert conditional debug statement in the code, Matlab needs to prepare to stop execution.

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by