Introduction
Speed of execution of code is an important issue for all coders. In this lecture we will discuss some programming practices to improve the performance of MATLAB code.
Analyzing Performance of the Code
MATLAB has some built is tools to help the coder to analyse the performance of his code. The two main tools are The M-file Profiler graphical user interface and the stopwatch timer. These tools help the coder to check information on how the program is performing
The M-File Profiler
Profiler help you to analyse the performance of the code and determine where you can modify your code to improve performance. M-file Profiler, a graphical user interface that shows you where your program is spending its time during execution. The Profiler can be more useful in measuring relative execution time and in identifying specific performance bottlenecks in your code.
Running the Profiler
Use one of the following methods:
- On the Editor tab, in the Run section, click Run and Time. If you use this method, the Profiler automatically profiles the code in the current Editor tab.
- On the Home tab, in the Code section, click Run and Time.
- In the Command Window, type profile viewer.
Stopwatch Timer Functions ( tic and toc)
Stopwatch functions is a tool which provide absolute time measurements of execution for your code.If you just need to get an idea of how long your program takes to run, or to compare the speed of different implementations of a program, you can use the stopwatch timer functions.. Invoking tic starts the timer, and the first subsequent toc stops it and reports the time elapsed between the two.
Syntax:
tic
run the program section to be timed
toc
Shorter programs will run too fast it may be difficult to use timer function. In this case, we can measure the time by running the program repeatedly in a loop, and then average to find the time for a single run.
ticfor k = 1:100
run the program
end
toc