Profile execution time for function
S = profile(
stats = profile('
profile function helps you debug and optimize MATLAB® code
files by tracking their execution time. For each MATLAB function, MATLAB local function, or MEX-function in
profile records information about execution
time, number of calls, parent functions, child functions, code line
hit count, and code line execution time. Some people use
to see the child functions;
that purpose. To open the Profiler graphical
user interface, use the
profile viewer syntax. By
default, Profiler time is CPU time. The total time reported by the
Profiler is not the same as the time reported using the
or the time you would observe using a stopwatch.
Note: If your system uses Intel® multi-core chips, you may want to restrict the active number of CPUs to 1 for the most accurate and efficient profiling. See Multi-Core Processors — Setting for Most Accurate Profiling on Windows Systems or Multi-Core Processors — Setting for Most Accurate Profiling on Linux Systems for details on how to do this.
starts the Profiler, clearing previously recorded profile statistics.
Note the following:
You can specify all, none, or a subset, of the
profile on syntax.
You can specify options in any order, including before
If the Profiler is currently on and you specify
one of the options, MATLAB software returns an error message
and the option has no effect. For example, if you specify
—timer real, MATLAB returns the following error:
The profiler has already been started. TIMER cannot
To change options, first specify
off, and then specify
profile on or
resume with new options.
records the exact sequence of function calls. The
records, by default, up to 1,000,000 function entry and exit events.
For more than 1,000,000 events,
to record other profile statistics, but not the sequence of calls.
To change the number of function entry and exit events that the
records, use the
By default, the
history option is not enabled.
disables further recording of the history (exact sequence of function
calls). Use the
-nohistory option after having
previously set the
-history option. All other profiling
statistics continue to be collected.
the number of function entry and exit events to record. By default,
set to 1,000,000.
specifies the type of time to use. Valid values for
'cpu' — The Profiler uses
computer time (the default).
'real' — The Profiler uses
cpu time for the
pause function is typically small, but
accounts for the actual time paused, and therefore would be larger.
specifies all of the options. Any order is acceptable, as is a subset.
stops the Profiler.
restarts the Profiler without clearing previously recorded statistics.
clears the statistics recorded by
stops the Profiler and displays the results in the Profiler window.
For more information, see Profiling for Improving
Performance in the Desktop Tools and Development Environment
S = profile(
returns a structure containing information about the current status
of the Profiler. The table lists the fields in the order that they
appear in the structure.
stats = profile(' stops
the Profiler and displays a structure containing the results. Use
this function to access the data generated by
The table lists the fields in the order that they appear in the structure.
Structure array containing statistics about each function called
Array containing function call history
Precision of the
Estimated clock speed of the CPU
Name of the profiler
FunctionTable field is an array of structures,
where each structure contains information about one of the functions
or local functions called during execution. The following table lists
these fields in the order that they appear in the structure.
Full path to
Function name; includes local functions
Full path to
MATLAB functions, MEX-functions, and many other types of functions including MATLAB local functions, nested functions, and anonymous functions
Number of times the function was called
Total time spent in the function and its child functions
No longer used.
Array containing line-by-line details for the function being profiled.
Column 1: Number of the line that executed. If a line was not executed, it does not appear in this matrix.
Column 2: Number of times the line was executed
Column 3: Total
time spent on that line. Note: The sum of Column 3 entries does not
necessarily add up to the function's
This example profiles the MATLAB
and then displays the results in the Profiler window. The example
then retrieves the profile data on which the HTML display is based
and uses the
profsave command to save the profile
data in HTML form.
profile on plot(magic(35)) profile viewer p = profile('info'); profsave(p,'profile_results')
Another way to save profile data is to store it in a MAT-file. This example stores the profile data in a MAT-file, clears the profile data from memory, and then loads the profile data from the MAT-file. This example also shows a way to bring the reloaded profile data into the Profiler graphical interface as live profile data, not as a static HTML page.
p = profile('info'); save myprofiledata p clear p load myprofiledata profview(0,p)
This example illustrates an effective way to view the results
of profiling when the
history option is enabled.
The history data describes the sequence of functions entered and exited
during execution. The
profile command returns history
data in the
FunctionHistory field of the structure
it returns. The history data is a 2-by-
The first row contains Boolean values, where
entrance into a function and
1 means exit from
a function. The second row identifies the function being entered or
exited by its index in the
This example reads the history data and displays it in the MATLAB Command
profile on -history plot(magic(4)); p = profile('info'); for n = 1:size(p.FunctionHistory,2) if p.FunctionHistory(1,n)==0 str = 'entering function: '; else str = 'exiting function: '; end disp([str p.FunctionTable(p.FunctionHistory(2,n)).FunctionName]) end