diff time.m @ 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 cb65c81d6c87
children
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