| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
tic; any_statements; toc;
tic; any_statements; tElapsed=toc;
tStart=tic; any_statements; toc(tStart);
tStart=tic; any_statements; tElapsed=toc(tStart);
tic; any_statements; toc; measures the time it takes the MATLAB software to execute the one or more lines of MATLAB code shown here as any_statements. The tic command starts a stopwatch timer, MATLAB executes the block of statements, and toc stops the timer, displaying the time elapsed in seconds.
tic; any_statements; tElapsed=toc; makes the same time measurement, but assigns the elapsed time output to a variable, tElapsed. MATLAB does not display the elapsed time unless you omit the terminating semicolon. The value returned by toc is a scalar double that represents the elapsed time in seconds.
tStart=tic; any_statements; toc(tStart); makes the same time measurement, but allows you the option of running more than one stopwatch timer concurrently. You assign the output of tic to a variable tStart and then use that same variable when calling toc. MATLAB measures the time elapsed between the tic and its related toc command and displays the time elapsed in seconds. This syntax enables you to time multiple concurrent operations, including the timing of nested operations.
tStart=tic; any_statements; tElapsed=toc(tStart); is the same as the command shown above, except that MATLAB assigns the elapsed time output to a variable, tElapsed. MATLAB does not display the elapsed time unless you omit the terminating semicolon. The value returned by toc is a scalar double that represents the elapsed time in seconds.
Using the third syntax shown above, you can nest tic-toc pairs.
When using the simpler tic and toc syntax, avoid using consecutive tics as they merely overwrite the internally-recorded starting time. Consecutive tocs however, may be useful as each toc returns the increasing time that has elapsed since the most recent tic. Using this mechanism, you can take multiple measurements from a single point in time.
When using the tStart=tic and toc(tStart) syntax, it is advisable to select a unique variable for tStart. If you accidentally overwrite this variable prior to the toc for which it is needed, you will get inaccurate results for the time measurement.
tStart is a 64-bit unsigned integer, scalar value. This value is only useful as an input argument for a subsequent call to toc.
The clear function does not reset the starting time recorded by a tic command.
Measure how the time required to solve a linear system varies with the order of a matrix:
for n = 1:100
A = rand(n,n);
b = rand(n,1);
tic
x = A\b;
t(n) = toc;
end
plot(t)Measure the minimum and average time to compute a summation of Bessel functions:
REPS = 1000; minTime = Inf; nsum = 10;
tic;
for i=1:REPS
tStart = tic; total = 0;
for j=1:nsum,
total = total + besselj(j,REPS);
end
tElapsed = toc(tStart);
minTime = min(tElapsed, minTime);
end
averageTime = toc/REPS;clock, cputime, etime, profile
![]() | throwAsCaller (MException) | Tiff class | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |