Skip to content

Instantly share code, notes, and snippets.

@jonathan-s
Created May 13, 2024 08:45
Show Gist options
  • Select an option

  • Save jonathan-s/d8b4a0a1dff6de7dd0af57f2c1c005a1 to your computer and use it in GitHub Desktop.

Select an option

Save jonathan-s/d8b4a0a1dff6de7dd0af57f2c1c005a1 to your computer and use it in GitHub Desktop.

Revisions

  1. jonathan-s created this gist May 13, 2024.
    18 changes: 18 additions & 0 deletions profile_cm.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    from pathlib import Path
    import cProfile


    class Profile:

    def __init__(self, path: Path):
    self.profiler = None
    self.path = path

    def __enter__(self):
    self.profiler = cProfile.Profile()
    self.profiler.enable()
    return self

    def __exit__(self, *args, **kwargs):
    self.profiler.dump_stats(self.path)
    self.profiler = None