Skip to content

Commit

Permalink
BUG: Fix average_of_best in loop_timer
Browse files Browse the repository at this point in the history
  • Loading branch information
oyamad committed Feb 3, 2018
1 parent cbfefc9 commit f49bd9b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
5 changes: 4 additions & 1 deletion quantecon/util/tests/test_timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import time
from numpy.testing import assert_allclose
from nose.tools import eq_
from nose.tools import eq_, ok_
from quantecon.util import tic, tac, toc, loop_timer


Expand Down Expand Up @@ -48,6 +48,9 @@ def test_function_two_arg(n, a):
for tm in test_two_arg:
assert(abs(tm - self.h) < 0.01)

for (average_time, average_of_best) in [test_one_arg, test_two_arg]:
ok_(average_time >= average_of_best)


if __name__ == '__main__':
import sys
Expand Down
11 changes: 5 additions & 6 deletions quantecon/util/timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""
import time
import numpy as np


class __Timer__:
Expand Down Expand Up @@ -143,15 +144,15 @@ def loop_timer(self, n, function, args=None, verbose=True, digits=2,
"""
tic()
all_times = []
all_times = np.empty(n)
for run in range(n):
if hasattr(args, '__iter__'):
function(*args)
elif args is None:
function()
else:
function(args)
all_times.append(tac(verbose=False, digits=digits))
all_times[run] = tac(verbose=False, digits=digits)

elapsed = toc(verbose=False, digits=digits)

Expand All @@ -161,10 +162,8 @@ def loop_timer(self, n, function, args=None, verbose=True, digits=2,
print("Total run time: %d:%02d:%0d.%0*d" %
(h, m, s, digits, (s % 1)*(10**digits)))

average_time = sum(all_times) / len(all_times)

best_times = all_times[:best_of]
average_of_best = sum(best_times) / len(best_times)
average_time = all_times.mean()
average_of_best = np.sort(all_times)[:best_of].mean()

if verbose:
m, s = divmod(average_time, 60)
Expand Down

0 comments on commit f49bd9b

Please sign in to comment.