]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/profshow.m
update packages
[CreaPhase.git] / octave_packages / m / general / profshow.m
1 ## Copyright (C) 2012 Daniel Kraft
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} profshow (@var{data})
21 ## @deftypefnx {Function File} {} profshow (@var{data}, @var{n})
22 ## Show flat profiler results.
23 ##
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}
29 ## defaults to 20.
30 ##
31 ## The attribute column shows @samp{R} for recursive functions and nothing
32 ## otherwise.
33 ## @seealso{profexplore, profile}
34 ## @end deftypefn
35
36 ## Built-in profiler.
37 ## Author: Daniel Kraft <d@domob.eu>
38
39 function profshow (data, n = 20)
40
41   if (nargin < 1 || nargin > 2)
42     print_usage ();
43   endif
44
45   n = fix (n);
46   if (! isscalar (n) || ! isreal (n) || ! (n > 0))
47     error ("profile: N must be a positive integer");
48   endif
49
50   m = length (data.FunctionTable);
51   n = min (n, m);
52
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 ];
57
58   [~, p] = sort (times);
59
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");
64   for i = 1 : n
65     nameLen = max (nameLen, length (data.FunctionTable(p(i)).FunctionName));
66   endfor
67   headerFormat = sprintf ("%%4s %%%ds %%4s %%12s %%12s\n", nameLen);
68   rowFormat = sprintf ("%%4d %%%ds %%4s %%12.3f %%12d\n", nameLen);
69
70   printf (headerFormat, "#", "Function", "Attr", "Time (s)", "Calls");
71   printf ("%s\n", repmat ("-", 1, nameLen + 2 * 5 + 2 * 13));
72   for i = 1 : n
73     row = data.FunctionTable(p(i));
74     attr = "";
75     if (row.IsRecursive)
76       attr = "R";
77     endif
78     printf (rowFormat, p(i), row.FunctionName, attr, ...
79             row.TotalTime, row.NumCalls);
80   endfor
81
82 endfunction
83
84 %!demo
85 %! profile ("on");
86 %! A = rand (100);
87 %! B = expm (A);
88 %! profile ("off");
89 %! T = profile ("info");
90 %! profshow (T, 10);
91
92 %!demo
93 %! profile ("on");
94 %! expm (rand (500) + eye (500));
95 %! profile ("off");
96 %! profshow (profile ("info"), 5);
97
98 %!error profshow ();
99 %!error profshow (1, 2, 3);
100 %!error profshow (struct (), 1.2);