Bench the given statement as many times as necessary until total executions take one second.
(name, cleanup=lambda: None, *, seconds=1, repeat=3)
| 19 | |
| 20 | |
| 21 | def 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 | |
| 41 | def from_cache(seconds, repeat): |
| 42 | """sys.modules""" |
no test coverage detected
searching dependent graphs…