Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions quantecon/util/timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time
import numpy as np
from ..timings.timings import get_default_precision
from numba import njit


class __Timer__:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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']
Expand Down