diff --git a/quantecon/util/timing.py b/quantecon/util/timing.py index 5ff9c8dc..bb1a45df 100644 --- a/quantecon/util/timing.py +++ b/quantecon/util/timing.py @@ -5,6 +5,7 @@ import time import numpy as np from ..timings.timings import get_default_precision +from numba import njit class __Timer__: @@ -100,10 +101,10 @@ def toc(self, verbose=True, digits=2): elapsed = t-self.start if verbose: - m, s = divmod(elapsed, 60) - h, m = divmod(m, 60) + h, m, s, subsec = _decompose_time(elapsed, digits) print("TOC: Elapsed: %d:%02d:%0d.%0*d" % - (h, m, s, digits, (s % 1)*(10**digits))) + (h, m, s, digits, subsec)) + return elapsed @@ -455,6 +456,19 @@ def toc(verbose=True, digits=2): def loop_timer(n, function, args=None, verbose=True, digits=2, best_of=3): return __timer__.loop_timer(n, function, args, verbose, digits, best_of) +@njit(cache=True, fastmath=True) +def _decompose_time(elapsed: float, digits: int): + """ + Helper to decompose elapsed time into hours, minutes, seconds, and fractional. + Numba-accelerated for performance. + """ + m, s = divmod(elapsed, 60) + h, m = divmod(m, 60) + # int required for formatting + subsec = int((s % 1)*(10**digits)) + # Cast to int for formatting the printf + return int(h), int(m), int(s), subsec + # Set docstring _names = ['tic', 'tac', 'toc', 'loop_timer']