Run a cprofile
(dump_path: Optional[str] = None)
| 101 | |
| 102 | @contextmanager |
| 103 | def profile(dump_path: Optional[str] = None): |
| 104 | """Run a cprofile""" |
| 105 | import cProfile |
| 106 | |
| 107 | prof = cProfile.Profile() |
| 108 | prof.enable() |
| 109 | |
| 110 | try: |
| 111 | yield |
| 112 | finally: |
| 113 | prof.disable() |
| 114 | if dump_path: |
| 115 | prof.dump_stats(dump_path) |
| 116 | else: |
| 117 | prof.print_stats(sort="cumtime") |
| 118 | |
| 119 | |
| 120 | @contextmanager |