主要内容

抽动

Start stopwatch timer

Description

example

抽动适用于TOCfunction to measure elapsed time. The抽动function records the current time, and theTOCfunction uses the recorded value to calculate the elapsed time.

example

Timerval= ticstores the current time inTimervalso that you can pass it explicitly to theTOCfunction. Passing this value is useful when there are multiple calls to抽动为了计时相同代码的不同部分。Timervalis an integer that has meaning only for theTOCfunction.

Examples

collapse all

测量创建两个随机矩阵所需的时间。

TIC A = RAND(12000,4400);b = rand(12000,4400);TOC
经过的时间为0.867440秒。

Measure the elapsed time since a call to the抽动function at different points of the program.

TIC A = RAND(12000,4400);b = rand(12000,4400);TOC
Elapsed time is 1.251803 seconds.
c = A.*b;TOC
Elapsed time is 1.404041 seconds.

Use a pair of抽动TOCcalls to report the total time required for element-by-element matrix multiplication; use another pair to report the total runtime of your program.

tStart = tic;%对2:抽动n = 10; T = zeros(1,n);fori = 1:n a = rand(12000,4400);b = rand(12000,4400);抽动%对1:抽动c = A.*b;T(i)= toc;% pair 1: tocendtMul = sum(T)
tMul = 0.5122
趋向= toc(tStart)% pair 2: toc
趋向= 12.7003

The variabletMulincludes the total time spent on multiplication.趋向specifies the elapsed time since the call to the抽动function at the beginning of the program.

Tips

  • Consecutive calls to the抽动function overwrite the internally recorded starting time.

  • The清除function does not reset the starting time recorded by a抽动function call.

  • 有时程序运行太快了抽动TOCto provide useful data. If your code is faster than 1/10 second, consider measuring it running in a loop, and then average to find the time for a single run. For more information, seeMeasure the Performance of Your Code

  • 以下操作导致意外输出:

    • Using抽动TOCto timetimeit
    • Using抽动TOC在一个定时的函数中timeit

Extended Capabilities

Version History

在R2006a之前引入