MCPcopy Index your code
hub / github.com/python/cpython / bench

Function bench

Tools/importbench/importbench.py:21–39  ·  view source on GitHub ↗

Bench the given statement as many times as necessary until total executions take one second.

(name, cleanup=lambda: None, *, seconds=1, repeat=3)

Source from the content-addressed store, hash-verified

19
20
21def bench(name, cleanup=lambda: None, *, seconds=1, repeat=3):
22 """Bench the given statement as many times as necessary until total
23 executions take one second."""
24 stmt = "__import__({!r})".format(name)
25 timer = timeit.Timer(stmt)
26 for x in range(repeat):
27 total_time = 0
28 count = 0
29 while total_time < seconds:
30 try:
31 total_time += timer.timeit(1)
32 finally:
33 cleanup()
34 count += 1
35 else:
36 # One execution too far
37 if total_time > seconds:
38 count -= 1
39 yield count // seconds
40
41def from_cache(seconds, repeat):
42 """sys.modules"""

Callers 9

_prepare_dataMethod · 0.85
from_cacheFunction · 0.85
builtin_modFunction · 0.85
source_wo_bytecodeFunction · 0.85
benchmark_wo_bytecodeFunction · 0.85
source_writing_bytecodeFunction · 0.85
source_using_bytecodeFunction · 0.85
using_bytecode_benchmarkFunction · 0.85

Calls 3

timeitMethod · 0.95
cleanupFunction · 0.70
formatMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…