Ctrl K
Java · Mid-level · Theory

A colleague wrote a microbenchmark: they call a method in a loop and measure the time with System.nanoTime. The results jump around, and the first runs are noticeably slower. Why does this happen, and how do you measure it properly?

~4 min by voice

How to approach it

The interviewer wants to see whether you understand how the JVM executes code (interpreter, JIT, tiered compilation) and why that makes a naive nanoTime measurement lie. The second part is whether you know the right tool (JMH) and its techniques: warm-up, forks, statistics, protection against dead code elimination.

Have you seen this question in a real interview?
Answers0
Avg. score—
Strong—
Practiced0
Topics
Similar questionsWhat happens to Java code from the moment you write it to the moment it runs? And what is a JIT compiler? JuniorTell me how JIT compilation works in HotSpot and what optimizations it performs. How does that affect the way you write and measure performance-critical code? Senior

Answer by voice

The interviewer will ask this question and listen. Think out loud and ask for a hint if you need one. When you're done, pause or tap “That's my answer”. In about a minute, your score and the points you covered show up here.

Sign in to answer

Best with headphones · ~4 min