Quick Tip
Your script is slow and you don't know why. Don't add print(time.time()) everywhere. Attach py-spy to the running process:
pip install py-spy
# Live flame graph of a running process (find PID with: pgrep -f myscript)
py-spy top --pid 12345
# Or record a flamegraph SVG for 30 seconds
py-spy record -o flame.svg --pid 12345 --duration 30
Zero code changes. Works on production processes, threads, and subprocesses (--subprocesses).
Real Example From Last Week
My CSV import job was taking 11 minutes. I assumed it was disk I/O. One py-spy record later:
| Function | % of samples |
|---|---|
csv.reader (actual parsing) |
12% |
re.sub inside my row-cleaning helper |
71% |
psycopg.execute |
9% |
The regex compiled on every row. Moving it to a module-level re.compile() dropped the job from 11 minutes to 3.
# Before: compiled 500,000 times
def clean(row):
return re.sub(r"\s+", " ", row["name"])
# After: compiled once
_WS = re.compile(r"\s+")
def clean(row):
return _WS.sub(" ", row["name"])
One line, 4x speedup, found in 30 seconds — with a tool that costs $0 while the "observability platform" quote for our side project started at $50/month.
What slow script are you going to point py-spy at first?
Written with help from MonkeyCode (free, local AI coding): https://ly.cyberserval.tech/iIETXiF
Top comments (0)