# HG changeset patch # User Rik # Date 1311373852 25200 # Node ID 031e1a2c26f3355bf7056c37de9677f5f89c8505 # Parent 23377c46516bdc0c25cd4ef0b92ae76c4e21bd57 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. diff --git a/scripts/general/profile.m b/scripts/general/profile.m --- a/scripts/general/profile.m +++ b/scripts/general/profile.m @@ -17,7 +17,7 @@ ## . ## -*- 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); diff --git a/scripts/general/profshow.m b/scripts/general/profshow.m --- a/scripts/general/profshow.m +++ b/scripts/general/profshow.m @@ -17,7 +17,7 @@ ## . ## -*- 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 -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);