Main Content

toc

Read elapsed time from stopwatch

Description

example

tocreads the elapsed time since the stopwatch timer started by the call to theticfunction. MATLAB®reads the internal time at the execution of thetocfunction and displays the elapsed time since the most recent call to theticfunction without an output. The elapsed time is expressed in seconds.

toc(timerVal)displays the elapsed time since the call to theticfunction corresponding totimerVal.

example

elapsedTime = tocreturns the elapsed time since the most recent call to theticfunction.

example

elapsedTime = toc(timerVal)returns the elapsed time since the call to theticfunction corresponding totimerVal.

Examples

collapse all

Measure the time required to create two random matrices.

tic A = rand(12000,4400); B = rand(12000,4400); toc
Elapsed time is 0.867440 seconds.

Measure the elapsed time since a call to theticfunction 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 ofticandtoccalls 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;% pair 2: ticn = 10; T = zeros(1,n);fori = 1:n A = rand(12000,4400); B = rand(12000,4400); tic% pair 1: ticC = A.*B; T(i)= toc;% pair 1: tocendtMul = sum(T)
tMul = 0.5122
tEnd = toc(tStart)% pair 2: toc
tEnd = 12.7003

The variabletMulincludes the total time spent on multiplication.tEndspecifies the elapsed time since the call to theticfunction at the beginning of the program.

Input Arguments

collapse all

Value of the internal timer saved from a previous call to theticfunction, specified as a scalar of typeuint64.

Tips

  • Consecutive calls to thetocfunction with no input return the elapsed time since the most recent call totic. This property enables you to take multiple measurements from a single point in time.

    Consecutive calls to thetocfunction with the sametimerValinput return the elapsed time since theticfunction call that corresponds totimerVal.

  • Sometimes programs run too fast forticandtocto 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.

  • The following actions result in unexpected output:

    • Usingticandtocto timetimeit
    • Usingticandtocwithin a function timed bytimeit

Extended Capabilities

版本嗨story

Introduced before R2006a