Mercurial > hg > octave-avbm
changeset 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 | e8c8e118a1e6 |
files | scripts/general/profile.m scripts/general/profshow.m |
diffstat | 2 files changed, 27 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/general/profile.m +++ b/scripts/general/profile.m @@ -17,7 +17,7 @@ ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} {} profile on +## @deftypefn {Function File} {} profile on ## @deftypefnx {Function File} {} profile off ## @deftypefnx {Function File} {} profile resume ## @deftypefnx {Function File} {} profile clear @@ -59,6 +59,10 @@ function retval = profile (option) + if (nargin != 1) + print_usage (); + endif + switch (option) case 'on' __profiler_reset (); @@ -87,11 +91,14 @@ retval = struct ('FunctionTable', data); otherwise + warning ("profile: Unrecognized option '%s'", option); print_usage (); + endswitch endfunction + %!demo %! profile ('on'); %! A = rand (100);
--- a/scripts/general/profshow.m +++ b/scripts/general/profshow.m @@ -17,7 +17,7 @@ ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} {} profshow (@var{data}) +## @deftypefn {Function File} {} profshow (@var{data}) ## @deftypefnx {Function File} {} profshow (@var{data}, @var{n}) ## Show flat profiler results. ## @@ -32,27 +32,30 @@ ## Built-in profiler. ## Author: Daniel Kraft <d@domob.eu> -function profshow (data, n) +function profshow (data, n = 20) - if (nargin < 2) - n = 20; + if (nargin < 1 || nargin > 2) + print_usage (); + endif + + n = fix (n); + if (! isscalar (n) || ! isreal (n) || ! (n > 0)) + error ("profile: N must be a positive integer"); endif m = length (data.FunctionTable); n = min (n, m); - % We want to sort by times in descending order. For this, extract the - % times to an array, then sort this, and use the resulting index permutation - % to print out our table. - times = NA (1, m); - for i = 1 : m - times(i) = - data.FunctionTable(i).TotalTime; - endfor + ## We want to sort by times in descending order. For this, extract the + ## times to an array, then sort this, and use the resulting index permutation + ## to print out our table. + times = -[ data.FunctionTable.TotalTime ]; + [~, p] = sort (times); - % For printing the table, find out the maximum length of a function name - % so that we can proportion the table accordingly. Based on this, - % we can build the format used for printing table rows. + ## For printing the table, find out the maximum length of a function name + ## so that we can proportion the table accordingly. Based on this, + ## we can build the format used for printing table rows. nameLen = length ('Function'); for i = 1 : n nameLen = max (nameLen, length (data.FunctionTable(p(i)).FunctionName)); @@ -61,10 +64,7 @@ rowFormat = sprintf ('%%%ds%%13.3f%%13d\n', nameLen); printf (headerFormat, 'Function', 'Time (s)', 'Calls'); - for i = 1 : nameLen + 2 * 13 - printf ('-'); - endfor - printf ('\n'); + printf ("%s\n", repmat ('-', 1, nameLen + 2 * 13)); for i = 1 : n row = data.FunctionTable(p(i)); printf (rowFormat, row.FunctionName, row.TotalTime, row.NumCalls); @@ -72,6 +72,7 @@ endfunction + %!demo %! profile ('on'); %! A = rand (100);