Mercurial > hg > octave-lyh
comparison scripts/general/profshow.m @ 12872:031e1a2c26f3
Vectorize and use Octave coding conventions for profile script files
* profile.m: Add nargin check at input. Add warning message for
unrecognized option.
* profshow.m: Add input validation for nargin and n. Use # instead
of % for comment character. Vectorize two for loops.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Fri, 22 Jul 2011 15:30:52 -0700 |
parents | 23377c46516b |
children | ca5c1115b679 |
comparison
equal
deleted
inserted
replaced
12871:23377c46516b | 12872:031e1a2c26f3 |
---|---|
15 ## You should have received a copy of the GNU General Public License | 15 ## You should have received a copy of the GNU General Public License |
16 ## along with Octave; see the file COPYING. If not, see | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | 17 ## <http://www.gnu.org/licenses/>. |
18 | 18 |
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {} profshow (@var{data}) | 20 ## @deftypefn {Function File} {} profshow (@var{data}) |
21 ## @deftypefnx {Function File} {} profshow (@var{data}, @var{n}) | 21 ## @deftypefnx {Function File} {} profshow (@var{data}, @var{n}) |
22 ## Show flat profiler results. | 22 ## Show flat profiler results. |
23 ## | 23 ## |
24 ## This command prints out profiler data as a flat profile. @var{data} is the | 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 | 25 ## structure returned by @code{profile ('info')}. If @var{n} is given, it |
30 ## @end deftypefn | 30 ## @end deftypefn |
31 | 31 |
32 ## Built-in profiler. | 32 ## Built-in profiler. |
33 ## Author: Daniel Kraft <d@domob.eu> | 33 ## Author: Daniel Kraft <d@domob.eu> |
34 | 34 |
35 function profshow (data, n) | 35 function profshow (data, n = 20) |
36 | 36 |
37 if (nargin < 2) | 37 if (nargin < 1 || nargin > 2) |
38 n = 20; | 38 print_usage (); |
39 endif | |
40 | |
41 n = fix (n); | |
42 if (! isscalar (n) || ! isreal (n) || ! (n > 0)) | |
43 error ("profile: N must be a positive integer"); | |
39 endif | 44 endif |
40 | 45 |
41 m = length (data.FunctionTable); | 46 m = length (data.FunctionTable); |
42 n = min (n, m); | 47 n = min (n, m); |
43 | 48 |
44 % We want to sort by times in descending order. For this, extract the | 49 ## We want to sort by times in descending order. For this, extract the |
45 % times to an array, then sort this, and use the resulting index permutation | 50 ## times to an array, then sort this, and use the resulting index permutation |
46 % to print out our table. | 51 ## to print out our table. |
47 times = NA (1, m); | 52 times = -[ data.FunctionTable.TotalTime ]; |
48 for i = 1 : m | 53 |
49 times(i) = - data.FunctionTable(i).TotalTime; | |
50 endfor | |
51 [~, p] = sort (times); | 54 [~, p] = sort (times); |
52 | 55 |
53 % For printing the table, find out the maximum length of a function name | 56 ## For printing the table, find out the maximum length of a function name |
54 % so that we can proportion the table accordingly. Based on this, | 57 ## so that we can proportion the table accordingly. Based on this, |
55 % we can build the format used for printing table rows. | 58 ## we can build the format used for printing table rows. |
56 nameLen = length ('Function'); | 59 nameLen = length ('Function'); |
57 for i = 1 : n | 60 for i = 1 : n |
58 nameLen = max (nameLen, length (data.FunctionTable(p(i)).FunctionName)); | 61 nameLen = max (nameLen, length (data.FunctionTable(p(i)).FunctionName)); |
59 endfor | 62 endfor |
60 headerFormat = sprintf ('%%%ds %%12s %%12s\n', nameLen); | 63 headerFormat = sprintf ('%%%ds %%12s %%12s\n', nameLen); |
61 rowFormat = sprintf ('%%%ds%%13.3f%%13d\n', nameLen); | 64 rowFormat = sprintf ('%%%ds%%13.3f%%13d\n', nameLen); |
62 | 65 |
63 printf (headerFormat, 'Function', 'Time (s)', 'Calls'); | 66 printf (headerFormat, 'Function', 'Time (s)', 'Calls'); |
64 for i = 1 : nameLen + 2 * 13 | 67 printf ("%s\n", repmat ('-', 1, nameLen + 2 * 13)); |
65 printf ('-'); | |
66 endfor | |
67 printf ('\n'); | |
68 for i = 1 : n | 68 for i = 1 : n |
69 row = data.FunctionTable(p(i)); | 69 row = data.FunctionTable(p(i)); |
70 printf (rowFormat, row.FunctionName, row.TotalTime, row.NumCalls); | 70 printf (rowFormat, row.FunctionName, row.TotalTime, row.NumCalls); |
71 endfor | 71 endfor |
72 | 72 |
73 endfunction | 73 endfunction |
74 | |
74 | 75 |
75 %!demo | 76 %!demo |
76 %! profile ('on'); | 77 %! profile ('on'); |
77 %! A = rand (100); | 78 %! A = rand (100); |
78 %! B = expm (A); | 79 %! B = expm (A); |