Warmup and Iterations
Measure steady-state performance.
Warmup and Iterations is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Warmup and Iterations
JMH measures steady-state performance: the speed your code reaches after the JVM has fully optimized it. To do that it runs throwaway warmup iterations first, then real measurement iterations. This lesson covers tuning both.
Why Warm Up
Early invocations run interpreted and are slow; after the JIT compiles the hot method, calls become much faster. Including the slow startup in your numbers would inflate them. Warmup iterations execute the code but their timings are discarded.
@Warmup
@Warmup controls the throwaway phase: how many iterations and how long each runs.
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
public class Bench {
@Benchmark
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
public int work() {
return 7 * 6;
}
}@Measurement
@Measurement controls the real, recorded phase. More iterations tighten the confidence interval but lengthen the run.
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
public class Bench {
@Benchmark
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
public int work() {
return 7 * 6;
}
}Iterations vs Operations
Be careful with terms. An iteration is a timed window (e.g. one second). Within each window JMH calls the benchmark method as many times as it can; each call is an operation. The score is reported per operation.
How Many Warmups Are Enough
Watch the per-iteration output. When successive warmup iterations stop changing, the JIT has stabilized. If the first few measurement iterations still drift, add more warmup. Five warmup and ten measurement iterations is a common starting point.
Forks Add More Warmup
Each fork is a brand-new JVM, so each fork repeats the full warmup. Running several forks both isolates JIT profiles and gives you variance across JVMs, which is the most honest measure of stability.
import org.openjdk.jmh.annotations.*;
@Fork(3)
@Warmup(iterations = 5)
@Measurement(iterations = 10)
public class Bench {
@Benchmark
public double sqrt() {
return Math.sqrt(2.0);
}
}Demonstrating the Effect
This plain Java loop prints per-batch timings. You will often see the first batches run slower than later ones as the JIT compiles the method — exactly the effect warmup discards.
public class Main {
static long busy(int n) {
long s = 0;
for (int i = 0; i < n; i++) s += (long) Math.sqrt(i);
return s;
}
public static void main(String[] args) {
for (int batch = 0; batch < 5; batch++) {
long t = System.nanoTime();
long r = busy(2_000_000);
System.out.println("batch " + batch + ": "
+ (System.nanoTime() - t) / 1000 + " us, r=" + r);
}
}
}Time-Based vs Count-Based
By default iterations are time-based (run for N seconds). You can switch to a fixed operation count with batchSize and single-shot modes, but time-based is the norm because it adapts to machine speed.
Don't Over-Tune
Very long runs cost time without improving accuracy much once steady-state is reached. Aim for stable, repeatable numbers across forks rather than maximizing iteration counts. Reproducibility beats a single impressive figure.
Reading Stabilization
JMH prints each iteration's score during the run. A healthy benchmark shows warmup iterations converging and measurement iterations clustered tightly around the final score with a small +/- error.
Quick Check
Test your understanding of warmup and iterations.
Recap
You learned to measure steady-state performance:
- @Warmup runs discarded iterations so the JIT can compile.
- @Measurement runs the recorded iterations; more tighten the error bar.
- An iteration is a timed window; each call inside it is an operation.
- Multiple @Forks repeat warmup in fresh JVMs and reveal cross-JVM variance.
- Aim for reproducible, stable scores rather than maximal run length.
Frequently asked questions
Is the “Warmup and Iterations” lesson free?
Yes — the full text of “Warmup and Iterations” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Warmup and Iterations”?
Measure steady-state performance. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Warmup and Iterations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why JMH
- Writing a Benchmark
- Warmup and Iterations
- Avoiding Dead-Code Elimination