1 ## Copyright (C) 2012 Daniel Kraft
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} profshow (@var{data})
21 ## @deftypefnx {Function File} {} profshow (@var{data}, @var{n})
22 ## Show flat profiler results.
24 ## This command prints out profiler data as a flat profile. @var{data} is the
25 ## structure returned by @code{profile ('info')}. If @var{n} is given, it
26 ## specifies the number of functions to show in the profile; functions are
27 ## sorted in descending order by total time spent in them. If there are more
28 ## than @var{n} included in the profile, those will not be shown. @var{n}
31 ## The attribute column shows @samp{R} for recursive functions and nothing
33 ## @seealso{profexplore, profile}
37 ## Author: Daniel Kraft <d@domob.eu>
39 function profshow (data, n = 20)
41 if (nargin < 1 || nargin > 2)
46 if (! isscalar (n) || ! isreal (n) || ! (n > 0))
47 error ("profile: N must be a positive integer");
50 m = length (data.FunctionTable);
53 ## We want to sort by times in descending order. For this, extract the
54 ## times to an array, then sort this, and use the resulting index permutation
55 ## to print out our table.
56 times = -[ data.FunctionTable.TotalTime ];
58 [~, p] = sort (times);
60 ## For printing the table, find out the maximum length of a function name
61 ## so that we can proportion the table accordingly. Based on this,
62 ## we can build the format used for printing table rows.
63 nameLen = length ("Function");
65 nameLen = max (nameLen, length (data.FunctionTable(p(i)).FunctionName));
67 headerFormat = sprintf ("%%4s %%%ds %%4s %%12s %%12s\n", nameLen);
68 rowFormat = sprintf ("%%4d %%%ds %%4s %%12.3f %%12d\n", nameLen);
70 printf (headerFormat, "#", "Function", "Attr", "Time (s)", "Calls");
71 printf ("%s\n", repmat ("-", 1, nameLen + 2 * 5 + 2 * 13));
73 row = data.FunctionTable(p(i));
78 printf (rowFormat, p(i), row.FunctionName, attr, ...
79 row.TotalTime, row.NumCalls);
89 %! T = profile ("info");
94 %! expm (rand (500) + eye (500));
96 %! profshow (profile ("info"), 5);
99 %!error profshow (1, 2, 3);
100 %!error profshow (struct (), 1.2);