annotate scripts/general/profshow.m @ 12871:23377c46516b

Implement the profshow function to print profiler results. * module.mk: Add profshow.m as new file. * profile.m: Use profshow in demo. * profshow.m: New function/file.
author Daniel Kraft <d@domob.eu>
date Wed, 20 Jul 2011 18:33:42 +0200
parents
children 031e1a2c26f3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12871
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
1 ## Copyright (C) 2011 Daniel Kraft
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
2 ##
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
3 ## This file is part of Octave.
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
4 ##
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
8 ## your option) any later version.
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
9 ##
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
13 ## General Public License for more details.
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
14 ##
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
18
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
19 ## -*- texinfo -*-
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
20 ## @deftypefn {Function File} {} profshow (@var{data})
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
21 ## @deftypefnx {Function File} {} profshow (@var{data}, @var{n})
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
22 ## Show flat profiler results.
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
23 ##
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
24 ## This command prints out profiler data as a flat profile. @var{data} is the
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
25 ## structure returned by @code{profile ('info')}. If @var{n} is given, it
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
26 ## specifies the number of functions to show in the profile; functions are
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
27 ## sorted in descending order by total time spent in them. If there are more
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
28 ## than @var{n} included in the profile, those will not be shown. @var{n}
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
29 ## defaults to 20.
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
30 ## @end deftypefn
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
31
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
32 ## Built-in profiler.
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
33 ## Author: Daniel Kraft <d@domob.eu>
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
34
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
35 function profshow (data, n)
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
36
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
37 if (nargin < 2)
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
38 n = 20;
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
39 endif
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
40
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
41 m = length (data.FunctionTable);
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
42 n = min (n, m);
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
43
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
44 % We want to sort by times in descending order. For this, extract the
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
45 % times to an array, then sort this, and use the resulting index permutation
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
46 % to print out our table.
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
47 times = NA (1, m);
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
48 for i = 1 : m
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
49 times(i) = - data.FunctionTable(i).TotalTime;
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
50 endfor
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
51 [~, p] = sort (times);
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
52
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
53 % For printing the table, find out the maximum length of a function name
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
54 % so that we can proportion the table accordingly. Based on this,
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
55 % we can build the format used for printing table rows.
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
56 nameLen = length ('Function');
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
57 for i = 1 : n
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
58 nameLen = max (nameLen, length (data.FunctionTable(p(i)).FunctionName));
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
59 endfor
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
60 headerFormat = sprintf ('%%%ds %%12s %%12s\n', nameLen);
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
61 rowFormat = sprintf ('%%%ds%%13.3f%%13d\n', nameLen);
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
62
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
63 printf (headerFormat, 'Function', 'Time (s)', 'Calls');
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
64 for i = 1 : nameLen + 2 * 13
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
65 printf ('-');
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
66 endfor
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
67 printf ('\n');
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
68 for i = 1 : n
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
69 row = data.FunctionTable(p(i));
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
70 printf (rowFormat, row.FunctionName, row.TotalTime, row.NumCalls);
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
71 endfor
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
72
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
73 endfunction
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
74
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
75 %!demo
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
76 %! profile ('on');
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
77 %! A = rand (100);
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
78 %! B = expm (A);
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
79 %! profile ('off');
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
80 %! T = profile ('info');
23377c46516b Implement the profshow function to print profiler results.
Daniel Kraft <d@domob.eu>
parents:
diff changeset
81 %! profshow (T, 10);