timeit
Measures the typical running time of a function.
Category: Software Development Tools > Performance and Memory
Syntax
t = timeit(f)
Returns the time, in seconds, that calling f() takes.
t = timeit(f, n)
Calls f requesting n output arguments.
Description
timeit runs the function several times, warms it up first and returns the median of the measurements, which is far less noisy than a single tic and toc. To time code that is not a single call, wrap it in an anonymous function, and move any setup outside the handle so it is not timed.
Timings differ between machines and runs, so never print the exact number in a test; compare orders of magnitude instead.
Input arguments
f- Function handle that takes no inputs.
n- Number of outputs to request from
f.
Output arguments
t- Scalar double, the measured time in seconds.
Example
Timing a function
A = magic(50);
t = timeit(@() A * A);
disp(class(t))
disp(t > 0)
Output
double
1