MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / Timer

Class Timer

tools/python-3.11.9-amd64/Lib/timeit.py:86–231  ·  view source on GitHub ↗

Class for timing execution speed of small code snippets. The constructor takes a statement to be timed, an additional statement used for setup, and a timer function. Both statements default to 'pass'; the timer function is platform-dependent (see module doc string). If 'globa

Source from the content-addressed store, hash-verified

84
85
86class Timer:
87 """Class for timing execution speed of small code snippets.
88
89 The constructor takes a statement to be timed, an additional
90 statement used for setup, and a timer function. Both statements
91 default to 'pass'; the timer function is platform-dependent (see
92 module doc string). If 'globals' is specified, the code will be
93 executed within that namespace (as opposed to inside timeit's
94 namespace).
95
96 To measure the execution time of the first statement, use the
97 timeit() method. The repeat() method is a convenience to call
98 timeit() multiple times and return a list of results.
99
100 The statements may contain newlines, as long as they don't contain
101 multi-line string literals.
102 """
103
104 def __init__(self, stmt="pass", setup="pass", timer=default_timer,
105 globals=None):
106 """Constructor. See class doc string."""
107 self.timer = timer
108 local_ns = {}
109 global_ns = _globals() if globals is None else globals
110 init = ''
111 if isinstance(setup, str):
112 # Check that the code can be compiled outside a function
113 compile(setup, dummy_src_name, "exec")
114 stmtprefix = setup + '\n'
115 setup = reindent(setup, 4)
116 elif callable(setup):
117 local_ns['_setup'] = setup
118 init += ', _setup=_setup'
119 stmtprefix = ''
120 setup = '_setup()'
121 else:
122 raise ValueError("setup is neither a string nor callable")
123 if isinstance(stmt, str):
124 # Check that the code can be compiled outside a function
125 compile(stmtprefix + stmt, dummy_src_name, "exec")
126 stmt = reindent(stmt, 8)
127 elif callable(stmt):
128 local_ns['_stmt'] = stmt
129 init += ', _stmt=_stmt'
130 stmt = '_stmt()'
131 else:
132 raise ValueError("stmt is neither a string nor callable")
133 src = template.format(stmt=stmt, setup=setup, init=init)
134 self.src = src # Save for traceback display
135 code = compile(src, dummy_src_name, "exec")
136 exec(code, global_ns, local_ns)
137 self.inner = local_ns["inner"]
138
139 def print_exc(self, file=None):
140 """Helper to print a traceback from the timed code.
141
142 Typical use:
143

Callers 4

timeitFunction · 0.70
repeatFunction · 0.70
mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected