This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Code Performance

Measure and profile MATLAB® code to improve performance


timeit Measure time required to run function
tic Start stopwatch timer
toc Read elapsed time from stopwatch
cputime Elapsed CPU time
profile Profile execution time for functions
bench MATLAB benchmark


Measure and Profile Code

Measure Performance of Your Program

To time how long your code takes to run, use the timeit function or the stopwatch timer functions, tic and toc.

Profile to Improve Performance

To identify which lines of code consume the most time or which lines MATLAB does not run, profile your code.

Use Profiler to Determine Code Coverage

When you run the Profiler on a file, some code might not run, such as a block containing an if statement. To determine how much of a file MATLAB executed when you profiled it, run the Coverage Report.

Improve Performance

Techniques to Improve Performance

To speed up the performance of your code, there are several techniques that you can consider.


A for or while loop that incrementally increases the size of an array each time through the loop can adversely affect performance and memory use. Often you can improve code execution time by preallocating the maximum amount of space required for the array.


You can revise loop-based, scalar-oriented code to use MATLAB matrix and vector operations. Vectorizing your code can make it easier to understand, less error prone, and faster to execute.

Was this topic helpful?