Start stopwatch timer
timerVal = tic
tic starts a stopwatch timer to measure
performance. The function records the internal time at execution of
tic command. Display the elapsed time with
the value of the internal timer at the execution of the
timerVal = tic
so that you can record time for simultaneous time spans.
Value of the internal timer at the execution of the
Measure time to generate two random matrices and compute element-by-element multiplication of their transposes.
tic A = rand(12000, 4400); B = rand(12000, 4400); toc C = A'.*B'; toc
Measure how the time required to solve a linear system varies with the order of a matrix:
t = zeros(1,100); for n = 1:100 A = rand(n,n); b = rand(n,1); tic; x = A\b; t(n) = toc; end plot(t)
Measure multiple time spans simultaneously using two pairs of
To do this, measure the minimum and average time to compute a summation
of Bessel functions:
REPS = 1000; minTime = Inf; nsum = 10; tic; % TIC, pair 1 for i=1:REPS tStart = tic; % TIC, pair 2 total = 0; for j=1:nsum total = total + besselj(j,REPS); end tElapsed = toc(tStart); % TOC, pair 2 minTime = min(tElapsed, minTime); end averageTime = toc/REPS; % TOC, pair 1
tic commands overwrite
the internally recorded starting time.
does not reset the starting time recorded by a
The following actions result in unexpected output:
tocwithin a function timed by
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
tic output value in MATLAB, or a MATLAB
tic output value in a MEX function.
If you call
tic in a MATLAB session and
toc 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, with
Hardware Board in the MATLAB
Coder™ app set to
MATLAB Host Computer.
When generating code on Windows® with Windows-compatible hardware settings, the generated C/C++
implementation uses the Windows API functions
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 to
199309L. The code
generator sets the macro to