Mercurial > repos > public > sbplib
changeset 195:305d8bb366ce
time: Added ability to avarage of several runs.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 26 May 2016 12:52:59 +0200 |
parents | 6db427e07edd |
children | 289fd8b1635c |
files | time.m |
diffstat | 1 files changed, 30 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/time.m Thu May 26 12:51:11 2016 +0200 +++ b/time.m Thu May 26 12:52:59 2016 +0200 @@ -1,10 +1,34 @@ -function t = time(f) +function t_out = time(f, n) + default_arg('n',1); + + if n == 1 + t = timeOnce(f); + else + t = timeAvg(f, n); + end + + if nargout == 1 + t_out = t; + else + fprintf('Elapsed time is %.6f seconds.\n', t) + end +end + +function t = timeOnce(f) s = tic(); f(); - if nargout == 1 - t = toc(s); - else - toc(s); -end \ No newline at end of file + t = toc(s); +end + + +function t = timeAvg(f, n) + s = tic(); + + for i = 1:n + f(); + end + + t = toc(s)/n; +end