Timing
MATLAB 中程序运行计时有以下方法
cputime Method
函数 cputime 返回 MATLAB 启动后 CPU 运行时间, 做差的程序 CPU 运行时间
tic/toc Method
tic, toc 作为一对函数称为 stopwatch timer functions. 基本语法如下
tic
% Code block
toctic 记录的时间自动传递给 toc, toc 再返回差值.
当有多对 tic/toc 时, 需要手动记录 tic 并传递给 toc.
timeit
函数 timeit 返回函数运行多次后的平均运行时间, 输入参数为函数句柄.
Remarks
- cputime 返回的是 CPU 时间; timeit 和 tic/toc 返回的是 wall-clock 时间
- CPU time for the pause function is typically small, but the wall-clock time accounts for the actual time that MATLAB execution is paused
- The cputime function measures the total CPU time and sums across all threads
- If your function uses four processing cores equally, the CPU time could be approximately four times higher than the wall-clock time
- Unlike tic/toc, the timeit function calls your code multiple times, and, therefore, considers first-time costs