gitmyhub

scalene

Python ★ 13k updated 4h ago

Scalene: a high-performance, high-precision CPU, GPU, and memory profiler for Python with AI-powered optimization proposals

A Python profiler from UMass Amherst that pinpoints exactly which lines of code are slow or wasting memory, with only 10-20% runtime overhead, far less than standard profilers, plus AI-powered fix suggestions.

PythonNumPyPyTorchCUDAsetup: easycomplexity 3/5

Scalene is a profiler for Python programs. A profiler is a tool that measures where a program spends its time and memory while running, so that you can find the slow or wasteful parts and improve them. Scalene was built by researchers at UMass Amherst and distinguishes itself from other Python profilers by being significantly faster and providing more detailed information at the same time.

Most profilers slow your program down by a large factor while measuring it, making results less realistic. Scalene uses a sampling approach rather than tracking every single function call, so its overhead is typically 10 to 20 percent. It also profiles at the individual line level, not just per function, so you can see exactly which line of code is consuming time or memory.

On the CPU side, Scalene separates out time spent in Python itself from time spent in native code, such as compiled libraries. This distinction matters because most developers can only optimize their own Python code, not the underlying library internals. It also identifies system time separately, which helps spot input and output bottlenecks. Hot spots are highlighted in red in the output.

Memory profiling is a notable strength. Scalene tracks which specific lines of code cause memory to grow, identifies likely memory leaks, and measures how much data is being copied across the Python and native code boundary. Accidental copies, such as converting a NumPy array into a plain Python list without realizing it, can be expensive, and Scalene flags those. GPU time is also reported, though currently limited to NVIDIA hardware.

The output can be viewed in a browser-based interface that opens automatically after a run, showing an interactive breakdown of CPU, GPU, and memory data per line. A command-line-only mode is also available. The tool also includes AI-powered optimization suggestions based on the profiling results.

Where it fits