Quick revision
The short answer
The loop has no warmup, so it measures the interpreter and JIT compilation mixed with compiled code. The result of parse is never used, so the JIT may remove the call as dead code, and the input is a constant, so parts of the work may be folded at compile time. It also runs once in one JVM, which gives no idea of variance. The fix is a JMH benchmark with @Benchmark that returns the result or feeds it to a Blackhole, reads its input from a non-final state field, and uses warmup, measurement iterations and several forks.
What to remember
In this answer, we saw four problems in a hand-written timing loop. With no warmup it mixes interpreter, compilation and compiled code. The unused result lets the JIT remove the call as dead code. The constant input lets it fold work at compile time. And one run in one JVM gives no estimate of variance. JMH fixes all of these with @Benchmark methods that return their result or use a Blackhole, inputs in non-final state fields, warmup and measurement iterations, and several forks.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The loop has no warmup, so it measures the interpreter and JIT compilation mixed with compiled code. The result of parse is never used, so the JIT may remove the call as dead code, and the input is a constant, so parts of the work may be folded at compile time. It also runs once in one JVM, which gives no idea of variance. The fix is a JMH benchmark with @Benchmark that returns the result or feeds it to a Blackhole, reads its input from a non-final state field, and uses warmup, measurement iterations and several forks.
Why this question is asked
Almost every Java developer has written this loop at some point to settle a question about which code is faster. The trouble is that the JVM is not a simple machine that runs our code as written: it interprets, profiles, compiles, inlines and removes code it can prove is useless. A naive timing loop measures some unknown mix of those activities, and the number it prints can be off by orders of magnitude. Interviewers ask this question to see whether we know why, and whether we reach for the right tool. In this answer, we will look at what the JVM does to this loop, go through each problem in turn, and rewrite it correctly with JMH.
1A Quick Recap: How the JVM Runs a Loop Like This
When the loop starts, parse runs in the interpreter, which executes bytecode one instruction at a time and is slow. The JVM counts how often methods and loops run. Once parse is called often enough, the C1 compiler produces a quick compiled version with profiling, and later the C2 compiler produces a heavily optimised version using that profile. A long-running loop inside main can even be compiled while it is running, a mechanism called on-stack replacement, OSR, which produces code that can differ from how the same method is compiled in normal use.
C2's optimisations are aggressive. It inlines small methods into their callers, propagates constants, and removes code whose result is never used. Each of these is good for real programs and dangerous for benchmarks.
2Problem 1: No Warmup
Here is the code from the question with what happens over time.
The single number averages the slow interpreted phase, the time spent compiling, and the fast compiled phase. If we doubled the iteration count, the result would change, because the slow start would weigh less. So the number describes neither cold startup nor steady state. A proper benchmark runs warmup iterations first, whose results are thrown away, until the code is compiled and stable, and only then measures.
3Problem 2: The Result Is Never Used
The loop calls parse and discards what it returns. If the JIT can inline parse and see that it has no side effects that matter, it is allowed to remove the call entirely, since nothing observes the result. This is dead code elimination. The loop then measures an empty loop, and the result might be a fraction of a nanosecond per call, which is obviously impossible for real date parsing but easy to mistake for a great result on simpler code.
Whether it actually removes the call depends on how much of parse can be inlined and proven side-effect free, which is exactly the problem: we cannot know from the output whether we measured the work or nothing. The fix is to make the result observable, by returning it from the benchmark method or passing it to a Blackhole, an object JMH provides that consumes values in a way the JIT cannot see through.
4Problem 3: The Input Is a Constant
The input "2024-01-01" is a literal, a compile-time constant. After inlining, the JIT knows the exact string passed in every iteration, and it can precompute parts of the work that depend only on that constant, such as its length, some character checks, or even a whole result in simple cases. This is constant folding. Real code parses different strings that arrive at runtime, so a benchmark with a folded constant measures something faster than reality.
The fix is to read the input from a field of a JMH state object that is not final. JMH treats such fields as values the JIT cannot assume constant, so the parsing really happens on each call.
5Problem 4: One Run Tells Us Nothing About Variance
Even a correct measurement varies from run to run. JIT compilation decisions, such as what gets inlined, can differ between JVM launches, and memory layout, garbage collection timing and other processes on the machine add noise. A single run in a single JVM gives one number with no idea whether the next run would give 200 or 260.
JMH addresses this with measurement iterations, several timed rounds within a JVM, and forks, several fresh JVM processes. It then reports the mean with an error margin. If two implementations differ by less than their error margins, we cannot claim either is faster.
| Problem | Effect on the number | JMH fix |
|---|---|---|
| No warmup | mixes interpreter, compile time, compiled code | @Warmup iterations, results discarded |
| Result unused | JIT may delete the call, near-zero time | return the value or use a Blackhole |
| Constant input | JIT may precompute work, too fast | non-final field in a @State class |
| One run, one JVM | no idea of variance | @Measurement iterations and @Fork |
| Loop in main, OSR | code compiled unlike real use | JMH generates the measurement loop |
6The Same Measurement Written With JMH
JMH, the Java Microbenchmark Harness, is the OpenJDK project's tool for exactly this. We write the code to measure as a method, and JMH generates the loop around it, handles warmup, forks and statistics, and protects against the optimisations above.
Now the result means something: the average time for one parse in steady state, over 15 measurements in three JVMs, with an error margin. If we want to compare with another implementation, we add a second @Benchmark method in the same class, and for realistic results we would use an array of varied dates rather than one string, so branch prediction and caches behave as they do in production.
7Follow-up questions
Is System.nanoTime itself a problem?
Not here, because it is called only twice. It becomes a problem when timing very short operations individually, since each call costs tens of nanoseconds and has limited resolution. JMH avoids this by timing batches of calls.
When is a simple timing loop acceptable?
For a rough feel of something that takes milliseconds or more, such as a whole batch job, after a warmup run, a simple timer is fine. For anything in the nanosecond or microsecond range, or for comparing two implementations, JMH is the right tool.
8Conclusion
In this answer, we saw four problems in a hand-written timing loop. With no warmup it mixes interpreter, compilation and compiled code. The unused result lets the JIT remove the call as dead code. The constant input lets it fold work at compile time. And one run in one JVM gives no estimate of variance. JMH fixes all of these with @Benchmark methods that return their result or use a Blackhole, inputs in non-final state fields, warmup and measurement iterations, and several forks.
Open with
There are several problems. First, there is no warmup: the first calls run in the interpreter, then C1 and C2 compile the code, possibly with on-stack replacement in the middle of the loop, so the number averages slow interpreted calls, compilation time and fast compiled calls, and it changes with the iteration count.
Then, if they want more
Second, the result of parse is thrown away, so once the JIT inlines it and sees no side effects, it can remove the call as dead code, and we might measure an empty loop.
Third, the input is a string literal, a constant, so after inlining the JIT can precompute part of the work, which real runtime input would not allow. Fourth, it is one run in one JVM, so there is no idea of variance; JIT decisions and noise differ between runs. The right tool is JMH.
I would write a @Benchmark method that returns LocalDate.parse of a non-final field in a @State class, so the result is consumed and the input is not constant, or use a Blackhole when there are several results.
Then configure warmup iterations, measurement iterations and at least a few forks, average time in nanoseconds, and only trust differences larger than the reported error. For realism I would also use varied inputs.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is wrong with this benchmark?": https://scrivio-interview-library.netlify.app/java/java-performance/aa6cd2398b/