Coding
AI Prompt for Loop Optimization
Optimize hot loops with this AI prompt: allocation elimination, cache-friendly ordering, and vectorization opportunities — with benchmarks to prove gains.
The prompt
Role: You are a low-level performance expert who makes hot loops fast. Context: Language: [LANGUAGE]. The hot loop: [CODE]. Iteration sizes: [SIZES]. What it computes: [PURPOSE]. Measured: [MEASUREMENT]. Task: 1) Identify loop inefficiencies: allocations inside the loop, redundant recomputation, cache-unfriendly access order, missed vectorization, branch mispredicts. 2) Rewrite the loop addressing them in impact order. 3) Explain WHY each change helps (cache lines, branch prediction, escape analysis — the hardware reason). 4) Provide a micro-benchmark to verify the gain. 5) State the readability cost honestly and whether it is worth it HERE. Format: Findings ranked by impact, then the rewritten loop, then the hardware explanations, then the benchmark, then the cost verdict. Ask the AI to show the assembly or IR hotspot for the innermost loop if your toolchain supports it. Seeing what the compiler actually emitted settles arguments about what 'should' be optimized. Constraints: Only optimize proven-hot loops — ask for [MEASUREMENT] if missing. Preserve exact numeric behavior (floating-point reorderings need explicit approval). Comment the non-obvious optimizations in the code.
Variables to fill in
Replace each [variable] in the prompt with your own details:
[LANGUAGE] [CODE] [SIZES] [PURPOSE] [MEASUREMENT]
Example input
LANGUAGE=Java, CODE=triple-nested loop over pixels with object allocation inside, SIZE=4096x4096 image, SYMPTOM=800ms per frame
How to use this prompt
Show the loop with its sizes — optimization depends on trip counts. State what the loop computes; the AI looks for redundant work across iterations.
Tips for the best results
- Profile first — the prompt demands measurement because intuition about hotspots is usually wrong.
- Ask about the cache-access order; it is the most common loop win.
- Get explicit approval before floating-point reassociation; it changes results subtly.
Frequently asked questions
Isn't the compiler supposed to handle this?
It restructures the hot loop (hoisting, allocation elimination, cache-friendly order) and explains WHY each change helps the hardware.
Will the compiler not do all of this anyway?
Compilers handle peepholes, not algorithms or memory layout. The prompt targets what compilers cannot: your data structures and access patterns.