comparison 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
comparison
equal deleted inserted replaced
194:6db427e07edd 195:305d8bb366ce
1 function t = time(f) 1 function t_out = time(f, n)
2 default_arg('n',1);
3
4 if n == 1
5 t = timeOnce(f);
6 else
7 t = timeAvg(f, n);
8 end
9
10 if nargout == 1
11 t_out = t;
12 else
13 fprintf('Elapsed time is %.6f seconds.\n', t)
14 end
15 end
16
17 function t = timeOnce(f)
2 s = tic(); 18 s = tic();
3 19
4 f(); 20 f();
5 21
6 if nargout == 1 22 t = toc(s);
7 t = toc(s);
8 else
9 toc(s);
10 end 23 end
24
25
26 function t = timeAvg(f, n)
27 s = tic();
28
29 for i = 1:n
30 f();
31 end
32
33 t = toc(s)/n;
34 end