Advanced

Finding the slow parts instead of guessing

Slow code rarely sits in one obvious place -- it hides in a loop you've written a hundred times without looking. When you ask the AI to help with performance, you're usually after two things: the slow spot, and proof that fixing it actually helped.

What you will see

Repeated loop seconds: 0.0191 Vectorized seconds: 0.0071 Optimized path is faster: True

About 2 min of reading

Try It

Find the slow parts and confirm them by timing

1

Spot repeated database calls and collapse them into one batched round-trip.

def repeated_db_calls(n): total = 0 for _ in range(n): total += 1 return total def batched_db_calls(n): return 1 # a single round-trip that fetches everything at once print() print("Performance: where is it slow?") n = 1000 before = repeated_db_calls(n) after = batched_db_calls(n) print("Repeated DB calls: %d round-trips" % before) print("Batched calls: %d round-trips" % after) print("Round-trips removed: %d" % (before - after))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Performance: where is it slow? Repeated DB calls: 1000 round-trips Batched calls: 1 round-trips Round-trips removed: 999
The AI can guess the culprit; confirm it by timing before and after.
2

Time a repeated loop against a single vectorized pass.

size = 1_000_000 data = [0.0] * size def repeated_loop(): total = 0.0 for x in data: total += x return total def vectorized(): return sum(data) import time t0 = time.perf_counter() repeated_loop() t1 = time.perf_counter() vectorized() t2 = time.perf_counter() print("Repeated loop seconds: %.4f" % (t1 - t0)) print("Vectorized seconds: %.4f" % (t2 - t1)) print("Optimized path is faster: True" if (t2 - t1) < (t1 - t0) else "Optimized path is faster: False")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Repeated loop seconds: 0.0191 Vectorized seconds: 0.0071 Optimized path is faster: True

Data Flow

Ask about speed, find the hot spot, apply a faster pattern, then time the result to be sure it helped.
1 Ask "where is slow?"
↓
2 Find the hot loop or call
↓
3 Apply a faster pattern
↓
4 Time the change
Why this works
Ask where it's slow

Tell the AI "where is this slow?" and let it point at repeated loops, repeated database calls, or data it rebuilds over and over. It can guess the cause, but you still get to confirm it.

Measure before you trust the number

The AI can name the likely culprit; it can't tell you whether it's actually the bottleneck. Time the code before and after the change. Only spend effort optimizing the parts that are genuinely slow.

Examples

The O(n squared) loop that sneaks in

AI: "This loop is O(n^2). Move the ids into a set for O(1) lookups."

for tag in all_tags:
    if tag in list_of_ids:   # scans the whole list every pass
        pass

id_set = set(list_of_ids)
for tag in all_tags:
    if tag in id_set:   # a single hash check, regardless of size
        pass

The original looks fine -- it even passes its tests. The cost only shows up when the data grows. Spotting the slow pattern is half the job; the set swap is the fix.

The query inside a loop

AI: "You run the same query once per row. That's N calls for N rows."
You: "How do I cut it down?"
AI: "Fetch everything in one query, then group the rows in memory."

for user in users:
    profile = db.get_profile(user.id)   # one database hit, per user

Every round-trip to the database has a cost, so N calls can add up fast. Pulling the data in one query and matching it up afterward often collapses the whole thing to a single call. It's one of the more satisfying fixes you'll make.

Building a string the slow way

AI: "You're adding strings together in a loop. That copies the whole thing every time."

text = ""
for line in lines:
    text = text + line + "\n"   # slow once the input is large

text = "\n".join(lines)   # one pass, one result

Concatenating strings in a loop is a classic speed trap, and it's easy to miss because it works. The join version builds the whole result in a single pass, which is both faster and a little more readable.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Getting the AI to find the security problems, not just the obvious ones
Previous Lesson
Getting the AI to find the security problems, not just the obvious ones
Next Lesson
Style that reads years from now
Style that reads years from now →