(
name: str, enable: bool = True, min_ms: float | None = None, profile: bool = False
)
| 29 | |
| 30 | @contextmanager |
| 31 | def timer( |
| 32 | name: str, enable: bool = True, min_ms: float | None = None, profile: bool = False |
| 33 | ): |
| 34 | if not enable: |
| 35 | yield |
| 36 | return |
| 37 | |
| 38 | if profile: |
| 39 | profiler = cProfile.Profile() |
| 40 | profiler.enable() |
| 41 | |
| 42 | start = time.time() |
| 43 | |
| 44 | yield |
| 45 | |
| 46 | end = time.time() |
| 47 | |
| 48 | if profile: |
| 49 | profiler.disable() |
| 50 | |
| 51 | ms = (end - start) * 1000 |
| 52 | if min_ms is None or ms > min_ms: |
| 53 | print(f"timer {name}: {ms:.2f}ms") |
| 54 | if profile: |
| 55 | profiler.print_stats() |
| 56 | |
| 57 | |
| 58 | @contextmanager |
nothing calls this directly
no outgoing calls
no test coverage detected