Vous êtes sur la page 1sur 2

5/8/2018 Read elapsed time from stopwatch - MATLAB toc - MathWorks India

toc
Read elapsed time from stopwatch

Syntax
toc
elapsedTime = toc
toc(timerVal)
elapsedTime = toc(timerVal)

Description
toc reads the elapsed time from the stopwatch timer started by the tic function. The function reads the internal time at the
execution of the toc command, and displays the elapsed time since the most recent call to the tic function that had no
output, in seconds.

elapsedTime = toc returns the elapsed time in a variable.

toc(timerVal) displays the time elapsed since the tic command corresponding to timerVal.

elapsedTime = toc(timerVal) returns the elapsed time since the tic command corresponding to timerVal.

Input Arguments
timerVal Value of the internal timer saved from a previous call to the tic command.

Output Arguments
elapsedTime Scalar double representing the time elapsed between tic and toc commands, in seconds.

Examples
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:

https://in.mathworks.com/help/matlab/ref/toc.html 1/2
5/8/2018 Read elapsed time from stopwatch - MATLAB toc - MathWorks India

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 tic/toc calls. 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

Tips
• Consecutive calls to the toc function with no input return the elapsed since the most recent tic. Therefore, you can
take multiple measurements from a single point in time.
Consecutive calls to the toc function with the same timerVal input return the elapsed time since the tic function call
that corresponds to that input.
• The following actions result in unexpected output:
˗ Using tic and toc to time timeit
˗ Using tic and toc within a function timed by timeit

See Also
clock | cputime | etime | profile | tic | timeit

Introduced before R2006a

https://in.mathworks.com/help/matlab/ref/toc.html 2/2

Vous aimerez peut-être aussi