<html lang="en"> <head> <title>Profiling - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Debugging.html#Debugging" title="Debugging"> <link rel="prev" href="Call-Stack.html#Call-Stack" title="Call Stack"> <link rel="next" href="Profiler-Example.html#Profiler-Example" title="Profiler Example"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Profiling"></a> <p> Next: <a rel="next" accesskey="n" href="Profiler-Example.html#Profiler-Example">Profiler Example</a>, Previous: <a rel="previous" accesskey="p" href="Call-Stack.html#Call-Stack">Call Stack</a>, Up: <a rel="up" accesskey="u" href="Debugging.html#Debugging">Debugging</a> <hr> </div> <h3 class="section">13.6 Profiling</h3> <p><a name="index-profiler-868"></a><a name="index-code-profiling-869"></a> 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. <p>The main command for profiling is <code>profile</code>, 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. <!-- profile scripts/general/profile.m --> <p><a name="doc_002dprofile"></a> <div class="defun"> — Command: <b>profile</b><var> on<a name="index-profile-870"></a></var><br> — Command: <b>profile</b><var> off<a name="index-profile-871"></a></var><br> — Command: <b>profile</b><var> resume<a name="index-profile-872"></a></var><br> — Command: <b>profile</b><var> clear<a name="index-profile-873"></a></var><br> — Function File: <var>S</var> = <b>profile</b> (<var>'status'</var>)<var><a name="index-profile-874"></a></var><br> — Function File: <var>T</var> = <b>profile</b> (<var>'info'</var>)<var><a name="index-profile-875"></a></var><br> <blockquote><p>Control the built-in profiler. <dl> <dt><code>profile on</code><dd>Start the profiler, clearing all previously collected data if there is any. <br><dt><code>profile off</code><dd>Stop profiling. The collected data can later be retrieved and examined with calls like <code>S = profile ('info')</code>. <br><dt><code>profile clear</code><dd>Clear all collected profiler data. <br><dt><code>profile resume</code><dd>Restart profiling without cleaning up the old data and instead all newly collected statistics are added to the already existing ones. <br><dt><var>S</var><code> = profile ('status')</code><dd>Return a structure filled with certain information about the current status of the profiler. At the moment, the only field is <code>ProfilerStatus</code> which is either 'on' or 'off'. <br><dt><var>T</var><code> = profile ('info')</code><dd>Return the collected profiling statistics in the structure <var>T</var>. The flat profile is returned in the field <code>FunctionTable</code> 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 <code>Hierarchical</code> contains the hierarchical call-tree. Each node has an index into the <code>FunctionTable</code> 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. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dprofshow.html#doc_002dprofshow">profshow</a>, <a href="doc_002dprofexplore.html#doc_002dprofexplore">profexplore</a>. </dl> </p></blockquote></div> <p>An easy way to get an overview over the collected data is <code>profshow</code>. This function takes the profiler data returned by <code>profile</code> as input and prints a flat profile, for instance: <pre class="example"> Function Attr Time (s) Calls ---------------------------------------- >myfib R 2.195 13529 binary <= 0.061 13529 binary - 0.050 13528 binary + 0.026 6764 </pre> <p>This shows that most of the run time was spent executing the function ‘<samp><span class="samp">myfib</span></samp>’, 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. <!-- profshow scripts/general/profshow.m --> <p><a name="doc_002dprofshow"></a> <div class="defun"> — Function File: <b>profshow</b> (<var>data</var>)<var><a name="index-profshow-876"></a></var><br> — Function File: <b>profshow</b> (<var>data, n</var>)<var><a name="index-profshow-877"></a></var><br> <blockquote><p>Show flat profiler results. <p>This command prints out profiler data as a flat profile. <var>data</var> is the structure returned by <code>profile ('info')</code>. If <var>n</var> 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 <var>n</var> included in the profile, those will not be shown. <var>n</var> defaults to 20. <p>The attribute column shows ‘<samp><span class="samp">R</span></samp>’ for recursive functions and nothing otherwise. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dprofexplore.html#doc_002dprofexplore">profexplore</a>, <a href="doc_002dprofile.html#doc_002dprofile">profile</a>. </p></blockquote></div> <!-- profexplore scripts/general/profexplore.m --> <p><a name="doc_002dprofexplore"></a> <div class="defun"> — Function File: <b>profexplore</b> (<var>data</var>)<var><a name="index-profexplore-878"></a></var><br> <blockquote><p>Interactively explore hierarchical profiler output. <p>Assuming <var>data</var> is the structure with profile data returned by <code>profile ('info')</code>, this command opens an interactive prompt that can be used to explore the call-tree. Type <kbd>help</kbd> to get a list of possible commands. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dprofile.html#doc_002dprofile">profile</a>, <a href="doc_002dprofshow.html#doc_002dprofshow">profshow</a>. </p></blockquote></div> </body></html>