Skip to content

Commit

Permalink
Add context manager to enable/disable profiling (#709)
Browse files Browse the repository at this point in the history
* add enable_profiling contextmanager

* Update scalene_profiler.py

Fixed spacing.

---------

Co-authored-by: Emery Berger <[email protected]>
  • Loading branch information
provinzkraut and emeryberger authored Jul 8, 2024
1 parent 49a26b7 commit 2aa5ebe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ scalene_profiler.start()
scalene_profiler.stop()
```

```Python
from scalene.scalene_profiler import enable_profiling

with enable_profiling():
# do something
```

</details>

<details>
Expand Down
9 changes: 8 additions & 1 deletion scalene/scalene_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
Set,
Tuple,
Union,
cast,
cast, Generator,
)

import scalene.scalene_config
Expand Down Expand Up @@ -147,6 +147,13 @@ def stop() -> None:
Scalene.stop()


@contextlib.contextmanager
def enable_profiling() -> Generator[None, None, None]:
"""Contextmanager that starts and stops profiling"""
start()
yield
stop()

class Scalene:
"""The Scalene profiler itself."""

Expand Down

0 comments on commit 2aa5ebe

Please sign in to comment.