Profiling is a way to measure where a program spends time. After you identify which functions are consuming the most time, you can evaluate them for possible performance improvements. Also, you can profile your code as a debugging tool. For example, determining which lines of code MATLAB® does not run can help you develop test cases that exercise that code. If you get an error in the file when profiling, you can see what ran and what did not to help you isolate the problem.
Code that is prematurely optimized can be unnecessarily complex without providing a significant gain in performance. Make your first implementation as simple as possible. Then, if speed is an issue, use profiling to identify bottlenecks.
You can profile your code using the MATLAB Profiler. The
Profiler is a user interface based on the results returned by the profile
function. If you are profiling
code that runs in parallel, for best results use the Parallel
Computing Toolbox™ parallel
profiler. For details, see Profiling Parallel Code (Parallel Computing Toolbox).
Use this general process to improve performance in your code:
Run the Profiler on your code.
In the Profile Summary report, look for functions that use a significant amount of time or that are called most frequently.
View the Profile Detail report for those functions, and look for the lines of code that take the most time or are called most often.
Consider keeping a copy of your first detail report as a basis for comparison. After you change your code, you can run the Profiler again and compare the reports.
Determine whether there are changes you can make to those lines of code to improve performance.
For example, if you have a load
statement
within a loop, you might be able to move the load
statement
outside the loop so that it is called only once.
Implement the potential performance improvements in your
code. Save the files, and run clear all
. Run the
Profiler again and compare the results to the original report.
If you profile the identical code twice, you can get slightly different results each time due to inherent time fluctuations that are not dependent on your code.
To continue improving the performance of your code, repeat these steps.
When your code spends most of its time on calls to a few built-in functions, you have probably optimized the code as much as possible.
To profile a MATLAB code file or a line of code:
Open the Profiler using one of the following methods:
In the Command Window, type profile viewer
.
On the Home tab, in the Code section,
click Run
and Time.
In the Editor, 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. If that is the code you want to profile,
skip to step 4.
In the Run this code field, type the statement you want to run.
For example, you can run the Lotka-Volterra example, which is provided with MATLAB:
[t,y] = ode23('lotka',[0 2],[20;20])
If, in the current MATLAB session, you previously profiled the statement, select it from the Run this code list. MATLAB automatically starts profiling the code, and you can skip to step 4.
Click Start Profiling.
While the Profiler is running, the Profile time indicator is green and the number of seconds it reports increases. The Profile time indicator appears at the top right of the Profiler window.
When the Profiler finishes, the Profile time indicator turns black and shows the length of time the Profiler ran. The statements you profiled display as having been executed in the Command Window.
This time is not the actual time that your statements took to
run. It is the time elapsed from when you clicked Start
Profiling until the profiling stops. If the time reported
is very different from what you expected (for example, hundreds of
seconds for a simple statement), you might have profiled longer than
necessary. This time does not match the time reported in Profile Summary
report statistics, which is based on performance
clock
time by default. To view profile statistics using a different type
of clock, use the profile
function instead of
the Profiler.
When profiling is complete, the Profile Summary report appears in the Profiler window. For more information, see Profile Summary Report.
To profile more than one statement:
In the Profiler, click Start Profiling. Make sure that no code appears in the Run this code field.
In the Command Window, enter and run the statements you want to profile.
After running all the statements, click Stop Profiling in the Profiler, and view the Profile Summary report.
You can run the Profiler for a user interface, such as the Filter Design and Analysis tool included with Signal Processing Toolbox™. Or, you can profile an interface you created, such as one built using GUIDE.
To profile a user interface:
In the Profiler, click Start Profiling. Make sure that no code appears in the Run this code field.
Start the user interface.
Use the interface. When you finish, click Stop Profiling in the Profiler, and view the Profile Summary report.
To exclude the user interface startup process in the profile, reverse steps 1 and 2. In other words, start the user interface before you click Start Profiling.
The Profile Summary report presents statistics about the overall execution of the function and provides summary statistics for each function called. The following is an image of the Profile Summary report for the Lotka-Volterra model. See Using the Profiler.
The Profile Summary report presents this information.
Column | Description |
---|---|
Function Name | List of all the functions called by the profiled code. Initially the functions appear in order of time they took to process. |
Calls | Number of times the profiled code called the function. |
Total Time | Total time spent in a function, including all accessed child functions, in seconds. The time for a function includes time spent in child functions. The Profiler itself takes some time, which is included in the results. The total time can be zero for files whose run time is inconsequential. |
Self Time | Total time in seconds spent in a function, excluding time spent in any child functions. Self time also includes some overhead resulting from the process of profiling. |
Total Time Plot | Graphic display showing self time compared to total time. |
In the summary report, you can:
Print the report, by clicking the print button .
Get more detailed information about a particular function by clicking its name in the Function Name column. For more information, see Profile Detail Report.
Sort by a given column by clicking the name of the column. For example, click the Function Name link to sort the functions alphabetically. Initially the results appear in order by Total Time.
The Profile Detail report shows profiling results for a function that MATLAB called while profiling.
To open the Profile Detail report, click a function name in
the Profile Summary report. To return to the Profile Summary report
from the Profile Detail report, click in the toolbar of the
Profile window.
The header of the Profile Detail report contains this information.
Name of the profiled function
Number of times the parent function called the profiled function
Time spent in the profiled function
Link to open the function in your default editor
Link to copy the report to a separate window. Saving a copy of the report is helpful to compare the impact of changes to your function. when you change the file.
To specify which sections the Profile Detail Report includes, select the check boxes at the top of the report, and click the Refresh button. Use the check boxes to select from these options.
Display Option | Details |
---|---|
Show parent functions | Display information about the parent functions, with links to their detail reports. To open a Profile Detail report for a parent function, click the name of the function. |
Show busy lines | List the lines in the profiled function that used the greatest amount of processing time. |
Show child functions | List all the functions called by the profiled function. To open a Profile Detail report for a child function, click the name of the function. |
Show Code Analyzer results | Display information about problems and potential improvements for the profiled function. |
Show file coverage | Display statistics about the lines of code in the function that MATLAB executed while profiling. |
Show function listing | Display the source code for the function, if it is a MATLAB code file. For each line of code, the Function listing includes these columns:
By default, the Profile Detail report highlights lines of code with the longest execution time. The darker the highlighting, the longer the line of code took to execute. To change the highlighting criteria, use the color highlight code drop-down list. |