Profile execution time for functions
Use the Profiler to track execution time. Knowing the execution time of your MATLAB® code helps you to debug and optimize it. For information on the user interface to the Profiler, see Profiling for Improving Performance.
the execution time for functions. Use
start, stop, and restart the Profiler, and view or clear profile statistics.
profile on starts the Profiler.
Turn on the Profiler, and call the
profile on n = 100; M = magic(n);
View the results in the Profiler window.
Save the results as HTML. By default,
the files to the
profile_results subfolder in your
current working folder.
Create the file
this main function and local function.
function c = myFunction(a,b) c = sqrt(square(a)+square(b)); end function y = square(x) y = x.^2; end
Turn on the Profiler, and enable the function call history
option. Profile a call to the
profile on -history a = rand(5); b = rand(5); c = myFunction(a,b);
Save the profile results.
p = profile('info')
p = FunctionTable: [2x1 struct] FunctionHistory: [2x6 double] ClockPrecision: 3.3475e-07 ClockSpeed: 3.0600e+09 Name: 'MATLAB' Overhead: 0
Display the function call history.
ans = 0 0 1 0 1 1 1 2 2 2 2 1
Display function entry and exit information by iterating over the function call history.
numEvents = size(p.FunctionHistory,2); for n = 1:numEvents name = p.FunctionTable(p.FunctionHistory(2,n)).FunctionName; if p.FunctionHistory(1,n) == 0 disp(['Entered ' name]); else disp(['Exited ' name]); end end
Entered myFunction Entered myFunction>square Exited myFunction>square Entered myFunction>square Exited myFunction>square Exited myFunction
Set the function call history to the default value.
s = profile('status')
s = ProfilerStatus: 'off' DetailLevel: 'mmex' Timer: 'performance' HistoryTracking: 'off' HistorySize: 1000000
action— Control options for Profiler
Control options for the Profiler specified as one of these options.
Start the Profiler, clearing any previously recorded profile statistics.
Stop the Profiler.
Restart the Profiler without clearing previously recorded statistics.
Clear the recorded statistics.
Stop the Profiler and display the results in the Profiler window. For more information, see Profile to Improve Performance.
Stop the Profiler and return a structure containing the results.
Return a structure with the Profiler status information.
option1 ... optionN— One or more profiling options
One or more profiling options, specified as character vectors corresponding to valid settings from the history and clock option tables. You can specify one or more of these options with one of the following syntaxes:
profile on option1 ... optionN
profile resume option1 ... optionN
profile option1 ... optionN
If you change the Profiler settings, the settings persist when you stop the Profiler or clear the statistics. To revert to default Profiler behavior, manually set the options to the default settings or start a new MATLAB session.
Record the exact sequence of function calls, including
function entry and exit events. By default, the
Specify the number of function entry and exit events
to record. By default,
Default setting. Disable recording of the exact sequence
of function calls. The
Default setting. Use wall-clock time from the clock that the operating system supplies to measure performance.
Use the wall-clock time directly from the processor. Sometimes your power savings settings or use of multiple processors influence this measurement.
Use system time reported by the operating system. This option is the most computationally expensive measurement and has the most impact on the performance of profiled code. Changing the time on the operating system clock influences this measurement.
Use computer time and sums time across all threads. This
measurement is different from wall-clock time. For
example, the computer time for the
p— Profiler statistics
Profiler statistics, returned as a structure containing these fields.
Function statistics, returned as a structure array. Each structure in the array contains information about one of the functions or local functions called during profiling. Each structure contains the following fields:
Function call history, returned as an array.
Precision of the time measurement of the
Estimated CPU clock speed, returned as a
Name of the profiler, returned as a character array.
Reserved for future use.
s— Profiler status
Profiler status, returned as a structure containing these fields.
To open the Profiler user interface,
profile viewer syntax or see Profiling for Improving
As of MATLAB R2015b, the default timer is
In previous versions of MATLAB, the default profiler timer was
which measures compute time instead of wall-clock time.