MCPcopy Create free account
hub / github.com/Atrovast/THGS / CodeTimer

Class CodeTimer

utils/linetimer.py:27–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25
26
27class CodeTimer:
28
29 def __init__(
30 self,
31 name: str = None,
32 silent: bool = False,
33 unit: str = UNIT_SECONDS,
34 logger_func=None,
35 dict_collect=None,
36 threshold: Optional[Union[int, float]] = None
37 ):
38 """
39 :param name: A custom name given to a code block
40 :param silent: When True, does not print or log any messages
41 :param unit: Units to measure time.
42 One of ['ns', 'us', 'ms', 's', 'm', 'h']
43 :param logger_func: A function that takes a string parameter
44 that is called at the end of the indented block.
45 If specified, messages will not be printed to console.
46 :param dict_collect: Return a dict with key=name, and val=time in `unit`
47 :param threshold: A integer or float value. If time taken by code block
48 took greater than or equal value, only then log.
49 If None, will bypass this parameter.
50 """
51
52 self.name = name
53 self.silent = silent
54 self.unit = unit
55 self.logger_func = logger_func
56 self.dict_collect = dict_collect
57 self.log_str = None
58 self.threshold = threshold
59
60 def __enter__(self):
61 """
62 Start measuring at the start of indent
63
64 :return: CodeTimer object
65 """
66
67 self.start = timeit.default_timer()
68
69 return self
70
71 def __exit__(self, exc_type, exc_value, traceback):
72 """
73 Stop measuring at the end of indent.
74 This will run even if the indented lines raise an exception.
75 """
76
77 # Record elapsed time in seconds
78 self.took = timeit.default_timer() - self.start
79
80 # Convert time into given units
81 self.took = (
82 self.took
83 / time_units.get(self.unit, time_units[UNIT_SECONDS])
84 )

Callers 3

everything_segFunction · 0.90
wrapperFunction · 0.85
linetimer.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected