tic/toc versus time.perf_counter_ns()

9 ビュー (過去 30 日間)
Mark Holmes
Mark Holmes 2022 年 6 月 8 日
回答済み: Peter Perkins 2022 年 6 月 13 日
For tic/toc the documentation indicates that these are limited to codes that take at least 1/10 second (if you want an accurate value). In python, the commands time.perf_counter_ns() and time.perf_counter() appear to have no such limitation. Is there a timer command for MATLAB without the 0.1 second rule?

回答 (1 件)

Peter Perkins
Peter Perkins 2022 年 6 月 13 日
The doc doesn't say you can't use tic/toc to time very short things, and clearly that is not true:
>> tic, x = 1; toc
Elapsed time is 0.000323 seconds.
What the doc does say is that if your total time is less than 0.1s, then you probably want to put whatever you are timing in a loop, and divide the total time by the number of iterations, otherwise your results are likely to be contaminated by noise. The timeit function is your friend.

カテゴリ

Find more on Startup and Shutdown in Help Center and File Exchange

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by