Programming and Data Types    

Techniques for Improving Performance

This section covers the following topics:

Analyzing Your Program's Performance

The M-file Profiler graphical user interface and the stopwatch timer functions enable you to get back information on how your program is performing and help you identify areas that need improvement.

The M-File Profiler

A good first step to speeding up your programs is to find out where the bottlenecks are. This is where you need to concentrate your attention to optimize your code.

MATLAB provides the M-file Profiler, a graphical user interface that shows you where your program is spending its time during execution. Use the Profiler to help you determine where you can modify your code to make performance improvements.

To start the Profiler, type profile viewer or select View -> Profiler in the MATLAB Command Window. See Measuring Performance for more information.

Stopwatch Timer Functions

If you just need to get an idea of how long your program (or a portion of it) takes to run, or to compare the speed of different implementations of a program, you can use the stopwatch timer functions, tic and toc. Invoking tic starts the timer, and the first subsequent toc stops it and reports the time elapsed between the two.

Use tic and toc as shown here.

Measuring Smaller Programs.   Shorter programs sometimes run too fast to get useful data from tic and toc. When this is the case, try measuring the program running repeatedly in a loop, and then average to find the time for a single run.

  Maximizing MATLAB Performance Vectorizing Loops