フィルターのクリア

Tic toc without output

12 ビュー (過去 30 日間)
Barbaros Teoman Kosoglu
Barbaros Teoman Kosoglu 2022 年 10 月 13 日
回答済み: John D'Errico 2022 年 10 月 13 日
I want to use tic toc to calculate time. But when the matrix is getting bigger the command window becomes unreadable. So is there a way to use tic toc without outputting the function.
calcDimTime(50)
calcDimTime(100)
calcDimTime(200)
calcDimTime(400)
function calcDimTime(n)
A = hilb(n);
tic
inv(A)
t = toc;
sprintf("The dimension of a is %f %f, and " + ...
"the time to calculate the inverse of A " + ...
"is %f",size(A),t)
end

採用された回答

Image Analyst
Image Analyst 2022 年 10 月 13 日
Add a semicolon at the end of the line:
inv(A);
  1 件のコメント
Barbaros Teoman Kosoglu
Barbaros Teoman Kosoglu 2022 年 10 月 13 日
Thank you.

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

その他の回答 (1 件)

John D'Errico
John D'Errico 2022 年 10 月 13 日
First, using tic and toc are bad ways to compute the time to do something. Why? They compute only ellapsed time, and even then, only poorly so. They compute the time for only one run of the code. Better to average things. Better to discard the first couple of times you call a code. Why? There is a warm-up needed, to get the true time. The first time you call a code, you also see the time needed to cache it, etc.
And of course, you need to make sure you are doing nothing else at the same time. Don't go surf the we, etc. That sucks time away from your CPU. Even in my case, I'm running MATLAB flat out right now to do a computation, one that is uusing one core of my CPU full time. So while I have an 8 core CPU, I can see if I start doing something one the side, as I am monitoring the time needed while it truns.
Anyway, MATLAB provides the timeit utility. USE IT!
If your problem is dumping crap in the command window, use semi-colons! All of this is avoided using timeit. So, we can do this:
N = 1000000;
tic,
p = primes(N);
toc
Elapsed time is 0.017321 seconds.
tic,
p = primes(N);
toc
Elapsed time is 0.006181 seconds.
tic,
p = primes(N);
toc
Elapsed time is 0.005911 seconds.
Or, this:
timeit(@() primes(N))
ans = 0.0033
The latter is going to be far more consistent. Do you see the significant variance in times reported from tic and toc?

カテゴリ

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

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by