Scalene: CPU, GPU, and Memory Profiler
Scalene is a high-resolution, low-overhead profiler for Python that analyzes CPU, GPU, and memory usage, providing AI-powered optimization suggestions. It's actively developed with frequent releases, typically addressing bug fixes, performance improvements, and compatibility updates for various Python versions and ecosystems.
Warnings
- gotcha Scalene explicitly requires Python versions `!=3.11.0` and `>=3.8`. This means Python 3.11.0 is not supported, though 3.11.1+ should be fine.
- breaking Earlier versions of Scalene (before 2.2.1) could crash with `SIGSEGV` when used with PyTorch Lightning on Python 3.12+ due to `sys.monitoring` tool ID conflicts.
- breaking TensorFlow 2.21+ changed `trace.enabled` from a callable to a boolean, causing compatibility issues with Scalene versions prior to 2.2.1.
- gotcha On Windows, Scalene v2.1.x could experience extreme slowness and memory explosion during CPU profiling due to a hardcoded 1ms sampling interval, overriding the configured rate.
- gotcha The primary and recommended way to use Scalene for profiling is via the command-line interface (`python -m scalene your_script.py`) or Jupyter magics (`%%scalene`). While a programmatic API exists, directly importing and calling functions to *run* the profiler in a general context is less common and might not capture the full execution scope as effectively.
Install
-
pip install scalene
Imports
- scalene_profiler
from scalene import scalene_profiler
Quickstart
# my_script.py
import time
import math
def calculate_heavy_stuff(n):
result = 0
for _ in range(n):
result += math.sin(math.sqrt(time.time()))
return result
def main():
print("Starting heavy calculations...")
calculate_heavy_stuff(1000000) # Perform 1 million iterations
print("Calculations finished.")
if __name__ == "__main__":
main()
# To run the profiler from your terminal:
# python -m scalene my_script.py