10 Prompts for Code Performance Optimization: From Profiling to Bottleneck Elimination

Why Performance Optimization Matters (and How Prompts Can Help)

Every developer has faced the moment when their code runs, but runs slowly. Finding the bottleneck often takes longer than writing the code itself. AI prompts, when crafted with domain knowledge, can act as a force multiplier — generating profiling scripts, suggesting algorithmic improvements, or even hinting at low‑level optimizations. This article presents 10 ready‑to‑use prompts organized by skill level: Basic (profiling and analysis), Advanced (architectural improvements), and Expert (hardware‑aware tuning). Each prompt includes a concrete task, the prompt text you can copy, and a realistic output.

Level Prompt Focus
Basic 1–3 Profiling, complexity, memory
Advanced 4–7 Data structures, I/O, parallelism
Expert 8–10 Cache, SIMD, low‑level extensions

Basic Prompts (Profiling & Analysis)

1. Profile Your Code to Identify Hotspots

Task: Generate a script that profiles a given Python function and reports the top 5 most time‑consuming lines.

Prompt:

Act as a performance engineer. Write a Python script that uses cProfile and pstats to profile the function below. The script should print the cumulative time per call, sorted by cumulative time. Function:

```python
def slow_function(n):
    total = 0
    for i in range(n):
        for j in range(1000):
            total += i * j
    return total

**Example result:** The AI outputs a profiling wrapper:
```python
import cProfile, pstats

profiler = cProfile.Profile()
profiler.enable()
slow_function(10000)
profiler.disable()
stats = pstats.Stats(profiler).sort_stats('cumulative')
stats.print_stats(5)

When run, it reveals that the inner loop total += i * j consumes over 90% of the time — pointing to the exact line that needs optimization.


2. Analyze Time Complexity (Big O)

Task: Ask the AI to compute the asymptotic complexity of a given algorithm and explain the reasoning.

Prompt:

You are a computer science tutor. Determine the time complexity of the following function in Big O notation. Provide a step‑by‑step analysis.

def example(arr):
    n = len(arr)
    for i in range(n):
        for j in range(n//2):
            print(arr[i])

Example result:

Time complexity: O(n²). Explanation:
- Outer loop runs n times.
- Inner loop runs n/2 times, which simplifies to n for big‑O.
- Combined: O(n * n) = O(n²).
- If the inner loop had a break condition, it could become O(n), but here it always runs n/2.

This quick analysis helps decide whether a rewrite is needed before deep profiling.


3. Memory Usage Analysis

Task: Generate a script that tracks memory allocation per line in Python using tracemalloc.

Prompt:

As a performance expert, write a Python snippet that uses tracemalloc to compare memory usage before and after running this function. Show the top 3 memory‑consuming lines.

def parse_large_file(filename):
    with open(filename) as f:
        data = f.readlines()
    return [line.strip().split(',') for line in data]

Example result:

import tracemalloc

tracemalloc.start()
parse_large_file('big.csv')
snapshot = tracemalloc.take_snapshot()
stats = snapshot.statistics('lineno')
for stat in stats[:3]:
    print(stat)

Output typically shows readlines() as the culprit — hinting at replacing it with an iterator to reduce peak memory.


Advanced Prompts (Architectural Improvements)

4. Detect Unnecessary Computations in Loops

Task: Ask the AI to review a loop and move invariant calculations outside.

Prompt:

You are a code reviewer. Identify any loop‑invariant computations in the following snippet and refactor them to improve performance. Explain each change.

```python
def inefficient(prices):
    result = []
    for price in prices:
        tax = 0.08 * 100 / (1 + 0.08)  # constant expression
        total = price * (1 + tax)
        result.append(total)
    return result

**Example result:**
```python
def improved(prices):
    tax_constant = 0.08 * 100 / (1 + 0.08)  # computed once
    result = []
    for price in prices:
        result.append(price * (1 + tax_constant))
    return result

Impact: In large loops this reduces CPU cycles by eliminating repeated floating‑point division.


5. Optimize Data Structures for Lookup Performance

Task: Get suggestions for replacing a list with a set or dict based on usage patterns.

Prompt:

Act as a software architect. The following code uses a list for membership checks. Suggest a more efficient data structure and rewrite the code accordingly.

```python
def check_duplicates(data):
    seen = []
    for item in data:
        if item in seen:
            return True
        seen.append(item)
    return False

**Example result:**
```python
def check_duplicates_optimized(data):
    seen = set()
    for item in data:
        if item in seen:
            return True
        seen.add(item)
    return False

Why: List lookup is O(n); set lookup is O(1). For large lists this can turn a O(n²) algorithm into O(n).


6. Parallelize Independent Tasks

Task: Ask the AI to rewrite a sequential loop with multiprocessing or threading.

Prompt:

You are a parallel computing specialist. Convert the following CPU‑intensive loop to use multiprocessing.Pool. Assume the function f is pure and expensive.

```python
def compute(data):
    return [f(x) for x in data]

**Example result:**
```python
from multiprocessing import Pool

def compute_parallel(data):
    with Pool() as pool:
        return pool.map(f, data)

Result: Near‑linear speedup on multi‑core machines. The prompt also warns about overhead for small datasets.


7. Reduce I/O Bottlenecks with Batching

Task: Generate a version of a file processor that uses buffered reads or asynchronous I/O.

Prompt:

As an I/O optimization expert, refactor this code to read files in batches instead of line‑by‑line, and suggest async alternatives.

```python
def process_logs(filepath):
    with open(filepath) as f:
        for line in f:
            if 'ERROR' in line:
                print(line)

**Example result:**
```python
def process_logs_batched(filepath, chunk_size=8192):
    with open(filepath, buffering=chunk_size) as f:
        for line in f:  # still line‑by‑line, but buffered I/O
            if 'ERROR' in line:
                print(line)

For further gains, the AI suggests using aiofiles for async reads when many files are involved.


Expert Prompts (Hardware‑Aware Tuning)

8. Loop Tiling for Cache Locality

Task: Ask the AI to apply loop tiling (blocking) to improve cache hit rate in a matrix multiplication.

Prompt:

You are a high‑performance computing specialist. Optimize the following matrix multiplication using loop tiling to maximize L1 cache usage. Explain the block size selection.

def matmul(A, B, n):
    C = [[0]*n for _ in range(n)]
    for i in range(n):
        for k in range(n):
            for j in range(n):
                C[i][j] += A[i][k] * B[k][j]
    return C

Example result:

def matmul_tiled(A, B, n):
    BLOCK = 32  # typical L1 size trade‑off
    C = [[0]*n for _ in range(n)]
    for i in range(0, n, BLOCK):
        for k in range(0, n, BLOCK):
            for j in range(0, n, BLOCK):
                for ii in range(i, min(i+BLOCK, n)):
                    for kk in range(k, min(k+BLOCK, n)):
                        for jj in range(j, min(j+BLOCK, n)):
                            C[ii][jj] += A[ii][kk] * B[kk][jj]
    return C

Result: Up to 10× speedup on large matrices due to data reuse in cache.


9. Leverage SIMD via Compiler Intrinsics (C/C++)
Task: Ask the AI to write an SSE/AVX intrinsic version of a vector addition.

Prompt:

Act as a low‑level performance engineer. Write a C function that adds two arrays of floats using AVX2 intrinsics. Include a simple benchmark.

Example result:

#include <immintrin.h>

void add_avx(float *a, float *b, float *c, int n) {
    int i;
    for (i = 0; i < n; i += 8) {
        __m256 va = _mm256_loadu_ps(a + i);
        __m256 vb = _mm256_loadu_ps(b + i);
        __m256 vc = _mm256_add_ps(va, vb);
        _mm256_storeu_ps(c + i, vc);
    }
}

Impact: Processes 8 floats per instruction instead of 1.


10. Use Cython to Accelerate a Hot Python Function

Task: Generate a Cython version of a computationally heavy function with static type declarations.

Prompt:

Act as a Cython expert. Convert the following pure Python loop into a Cython module with static types. Focus on the tight inner loop.

```python
def sum_squares(n):
    total = 0
    for i in range(n):
        total += i * i
    return total

**Example result (sum_squares.pyx):**
```cython
def sum_squares(int n):
    cdef long long total = 0
    cdef int i
    for i in range(n):
        total += i * i
    return total

Speed: 10–40× faster than pure Python, no external dependencies.


Conclusion

Performance optimization is a mix of art and science. The 10 prompts above give you a starting point — from basic profiling with cProfile to hardware‑aware techniques like loop tiling and SIMD. The key is to first measure, then optimize. Copy the prompts, adapt them to your language and framework, and let the AI handle the boilerplate. Remember: the best optimizations are those you can verify with actual benchmarks. Try these prompts on your next project and share your speedups.

What’s your favorite performance trick? Let us know in the comments!

← All posts

Comments