Next: Profiler Example, Previous: Call Stack, Up: Debugging [Contents][Index]

### 13.6 Profiling

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.

Command: profile on
Command: profile off
Command: profile resume
Command: profile clear
Function File: S = profile ("status")
Function File: T = profile ("info")

  Control the built-in profiler.

  `profile on`

    Start the profiler, clearing all previously collected data if there is any.

  `profile off`

    Stop profiling. The collected data can later be retrieved and examined with calls like `S = profile ("info")`.

  `profile clear`

    Clear all collected profiler data.

  `profile resume`

    Restart profiling without cleaning up the old data and instead all newly collected statistics are added to the already existing ones.

  `S = profile ("status")`

    Return a structure filled with certain information about the current status of the profiler. At the moment, the only field is `ProfilerStatus` which is either `"on"` or `"off"`.

  `T = profile ("info")`

    Return the collected profiling statistics in the structure T. The flat profile is returned in the field `FunctionTable` which is an array of structures, each entry corresponding to a function which was called and for which profiling statistics are present. Furthermore, the field `Hierarchical` contains the hierarchical call-tree. Each node has an index into the `FunctionTable` identifying the function it corresponds to as well as data fields for number of calls and time spent at this level in the call-tree.

    See also: profshow, profexplore.

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.

Function File: profshow (data)
Function File: profshow (data, n)

  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.

  See also: profexplore, profile.

Function File: profexplore ()
Function File: profexplore (data)

  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. If data is omitted, `profile ("info")` is called and used in its place.

  See also: profile, profshow.

Next: Profiler Example, Previous: Call Stack, Up: Debugging [Contents][Index]