Matlab - Using Profile to Find Bottlenecks in Your Code
16 Jan 2008 Quan Quach 2 comments 625 views
Introduction
A very useful feature within Matlab is the profile command. The profile function helps you debug and optimize M-files by tracking their execution time. The purpose of this tutorial is to show you how to use the profile command to find bottlenecks in your code.
Example: Use Profile to Find the Bottleneck
-
Let’s assume that we have the following function. Copy and paste the code into the m-editor and save it as an m-file (filename: test).
function myVector = test(count) myVector = 1:count; %row vector that goes from 1 to count for x=1:length(myVector) if(mod(myVector(x),2)) %if the element is even myVector(x) = cot(myVector(x)); %we take the cotangent of it end end
-
Next, lets use the profile command. Copy and paste this into the command prompt:
profile on %turns on the profile profile clear %clears any previous information test(100000); %runs the function profreport %brings up the profile report
-
You should see the profiler window appear which gives as quick summary. Click on “test”.

-
Now you should see a more detailed summary of the function. Of particular interest is where the most time was spent running the code. As can be seen, the cot line took up the largest percentage of time. That must be the bottleneck!

-
Let’s also go to the bottom of the profile listing, to where the “Function Listing” is contained. It gives a breakdown of the amount of time spent on each line and how many calls were made to that line. This just further affirms that the cot line is the culprit.

Optimizing the Code
-
So what is the best way to fix this bottleneck? Vectorization is the key! Copy and paste the following code into the m-editor and save it in the same place as the previous m-file (filename: test2). This function will return exactly the same results as the previous function.
function myVector = test2(count) myVector = 1:count; %row vector that goes from 1 to count %get the indices that are even indexTrue = mod(myVector,2)==1; %take the cotangent of the even elements myVector(indexTrue) = cot(myVector(indexTrue));
-
Next, run the following code at the command prompt.
profile on %turns on the profile profile clear %clears any previous information test(100000); %runs the function test2(100000); %runs the function profreport %brings up the profile report
-
Here’s what you should see. Notice the total time for test2 is much less than the total time for test1. It looks like vectorization was able to solve the bottleneck!

A Word of caution on Optimzation
Optimization can speed up your code and save valuable processing time, but you must ask yourself if it is really necessary. The first priority is to always make sure that the code does what you want it to do. Once you have it written and working, then you can consider some optimization routines. Optimizing can lead to more errors, so be careful what you optimize. Make sure the portion of code that you want to optimize is a speed bottleneck before you proceed with the optimization. Good Luck!
This is the end of the tutorial.
2 Responses to “Matlab - Using Profile to Find Bottlenecks in Your Code”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


Thanks. This was very helpful.
[...] Matlab - Using Profile to Find Bottlenecks in Your Code | blinkdagger [...]