tic - Start stopwatch timer - MATLAB (original) (raw)
Main Content
Syntax
Description
tic
works with the toc
function to measure elapsed time. The tic
function records the current time, and the toc
function uses the recorded value to calculate the elapsed time.
timerVal = tic
stores the current time intimerVal
so that you can pass it explicitly to thetoc
function. Passing this value is useful when there are multiple calls to tic
to time different parts of the same code.timerVal
is an integer that has meaning only for thetoc
function.
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.
Tips
- Consecutive calls to the
tic
function overwrite the internally recorded starting time. - The clear function does not reset the starting time recorded by a
tic
function call. - 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
tic
is different between MATLAB® and the generated code. To avoid errors, do not use a MEX-functiontic
output value in MATLAB, or a MATLABtic
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
tic
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