Octave supports profiling of code execution on a per-function level. If profiling is enabled, each call to a function (supporting built-ins, operators, functions in oct- and mex-files, user-defined functions in Octave code and anonymous functions) is recorded while running Octave code. After that, this data can aid in analyzing the code behavior, and is in particular helpful for finding “hot spots” in the code which use up a lot of computation time and are the best targets to spend optimization efforts on.
The main command for profiling is
profile, which can be used to
start or stop the profiler and also to query collected data afterwards.
The data is returned in an Octave data structure which can then be
examined or further processed by other routines or tools.
Control the built-in profiler.
- Start the profiler, clearing all previously collected data if there is any.
- Stop profiling. The collected data can later be retrieved and examined with calls like
S = profile ('info').
- Clear all collected profiler data.
- Restart profiling without cleaning up the old data and instead all newly collected statistics are added to the already existing ones.
= profile ('status')
- Return a structure filled with certain information about the current status of the profiler. At the moment, the only field is
ProfilerStatuswhich is either 'on' or 'off'.
= profile ('info')
- Return the collected profiling statistics in the structure T. The flat profile is returned in the field
FunctionTablewhich is an array of structures, each entry corresponding to a function which was called and for which profiling statistics are present. Furthermore, the field
Hierarchicalcontains the hierarchical call-tree. Each node has an index into the
FunctionTableidentifying the function it corresponds to as well as data fields for number of calls and time spent at this level in the call-tree.
An easy way to get an overview over the collected data is
profshow. This function takes the profiler data returned by
profile as input and prints a flat profile, for instance:
Function Attr Time (s) Calls ---------------------------------------- >myfib R 2.195 13529 binary <= 0.061 13529 binary - 0.050 13528 binary + 0.026 6764
This shows that most of the run time was spent executing the function ‘myfib’, and some minor proportion evaluating the listed binary operators. Furthermore, it is shown how often the function was called and the profiler also records that it is recursive.
Show flat profiler results.
This command prints out profiler data as a flat profile. data is the structure returned by
profile ('info'). If n is given, it specifies the number of functions to show in the profile; functions are sorted in descending order by total time spent in them. If there are more than n included in the profile, those will not be shown. n defaults to 20.
The attribute column shows ‘R’ for recursive functions and nothing otherwise.
Interactively explore hierarchical profiler output.
Assuming data is the structure with profile data returned by
profile ('info'), this command opens an interactive prompt that can be used to explore the call-tree. Type help to get a list of possible commands.