MATLAB Language Performance and Benchmarking Identifying performance bottlenecks using the Profiler


The MATLAB Profiler is a tool for software profiling of MATLAB code. Using the Profiler, it is possible to obtain a visual representation of both execution time and memory consumption.

Running the Profiler can be done in two ways:

  • Clicking the "Run and Time" button in the MATLAB GUI while having some .m file open in the editor (added in R2012b).

    Button in toolstrip

  • Programmatically, using:

    profile on
    <some code we want to test>
    profile off

Below is some sample code and the result of its profiling:

function docTest

for ind1 = 1:100
  [~] = var(...


Profiler output

From the above we learn that the spy function takes about 25% of the total execution time. In the case of "real code", a function that takes such a large percentage of execution time would be a good candidate for optimization, as opposed to functions analogous to var and cla whose optimization should be avoided.

Moreover, it is possible to click on entries in the Function Name column to see a detailed breakdown of execution time for that entry. Here's the example of clicking spy:

Internal timing of "spy"

It is also possible to profile memory consumption by executing profile('-memory') before running the Profiler.

enter image description here