toc - Read elapsed time from
stopwatch - MATLAB (original) (raw)
Main Content
Read elapsed time from stopwatch
Syntax
Description
toc
reads the elapsed time since the stopwatch timer started by the call to the tic
function. MATLAB® reads the internal time at the execution of the toc
function and displays the elapsed time since the most recent call to thetic
function without an output. The elapsed time is expressed in seconds.
toc([timerVal](#bswh%5Fpb-1-timerVal))
displays the elapsed time since the call to the tic
function corresponding totimerVal
.
elapsedTime = toc
returns the elapsed time since the most recent call to the tic
function.
elapsedTime = toc([timerVal](#bswh%5Fpb-1-timerVal))
returns the elapsed time since the call to the tic
function corresponding totimerVal
.
Examples
Measure the time required to create two random matrices.
tic A = rand(12000,4400); B = rand(12000,4400); toc
Elapsed time is 0.805683 seconds.
Measure the elapsed time since a call to the tic
function at different points of the program.
tic A = rand(12000,4400); B = rand(12000,4400); toc
Elapsed time is 0.769707 seconds.
Elapsed time is 0.860179 seconds.
Use a pair of tic
and toc
calls 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: tic n = 10; T = zeros(1,n); for i = 1:n A = rand(12000,4400); B = rand(12000,4400); tic % pair 1: tic C = A.*B; T(i)= toc; % pair 1: toc end tMul = sum(T)
tEnd = toc(tStart) % pair 2: toc
The variable tMul
includes the total time spent on multiplication. tEnd
specifies the elapsed time since the call to the tic
function at the beginning of the program.
Input Arguments
Value of the internal timer saved from a previous call to thetic
function, specified as a scalar of typeuint64
.
Tips
- Consecutive calls to the
toc
function 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 thetoc
function with the sametimerVal
input return the elapsed time since thetic
function call that corresponds totimerVal
. - Sometimes programs run too fast for
tic
andtoc
to 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, see Measure the Performance of Your Code. - The following actions result in unexpected output:
- Using
tic
andtoc
to timetimeit - Using
tic
andtoc
within a function timed bytimeit
- Using
Extended Capabilities
Usage notes and limitations:
- The data type returned from
toc
is different between MATLAB and the generated code. To avoid errors, do not use a MEX-functiontoc
output value in MATLAB, or a MATLABtoc
output value in a MEX function. - If you call
tic
in a MATLAB session andtoc
in a MEX function, or vice versa, the timing results are not coordinated. - The C/C++ implementation for
toc
in the generated code differs depending on the hardware settings stored in the code generation configuration object. By default, the hardware settings are configured for the host platform, withHardware Board
in the MATLAB Coder™ app set toMATLAB Host Computer
.- When generating code on Windows® with Windows-compatible hardware settings, the generated C/C++ implementation uses the Windows API functions
QueryPerformanceFrequency
andQueryPerformanceCounter
. - In all other cases, the implementation uses the POSIX API
clock_gettime
. When compiling code that uses the POSIX API, the preprocessor macro_POSIX_C_SOURCE
must be set to an integer greater than or equal to199309L
. The code generator sets the macro to199309L
for compilation.
- When generating code on Windows® with Windows-compatible hardware settings, the generated C/C++ implementation uses the Windows API functions
Version History
Introduced before R2006a