How do I measure time elapsed in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To measure elapsed time in Java, the usual tool is System.nanoTime(), not System.currentTimeMillis(). The key distinction is that elapsed-time measurement needs a monotonic clock, while wall-clock timestamps are about calendar time and can move because of clock adjustments.
Use System.nanoTime() for Elapsed Time
For timing how long code takes to run, System.nanoTime() is the standard choice.
Despite the name, the returned value is not a wall-clock timestamp. It is a monotonic time source intended for measuring intervals. That is exactly why it is better for elapsed time than currentTimeMillis().
Why System.currentTimeMillis() Is Not Ideal
System.currentTimeMillis() returns wall-clock time since the Unix epoch.
This can work for rough timing, but it has two problems:
- lower effective resolution on many systems
- sensitivity to system clock changes
If the OS clock is adjusted while your code runs, the elapsed calculation may be misleading. That is why currentTimeMillis() is for timestamps, not serious duration measurement.
Convert Nanoseconds to Readable Units
Raw nanoseconds are precise, but not always convenient to display directly.
This is often enough for logging and debugging. For user-facing or report-style code, wrapping the interval in Duration can be clearer.
Instant and Duration for Readability
If readability matters more than low-level timing detail, java.time can be a good fit.
This is pleasant to read, but remember that Instant.now() is still tied to the system clock rather than the monotonic timer that powers nanoTime(). For benchmarking or tight performance measurement, nanoTime() remains the safer default.
Timing Repeated Operations
One run is often noisy because of JIT compilation, cache effects, class loading, and background system activity. If you want a more meaningful measurement, repeat the operation and aggregate the results.
This is still only a rough benchmark, but it is better than trusting one timing sample.
For Real Benchmarking, Use JMH
If you are benchmarking performance seriously, use JMH instead of handwritten loops. JMH handles warm-up, dead-code elimination risks, and measurement rigor much better than ad hoc timing snippets.
Handwritten timing is fine for diagnostics, logs, and rough comparisons. It is not a replacement for a real microbenchmark framework when performance claims matter.
Common Pitfalls
The most common mistake is using System.currentTimeMillis() for elapsed time and assuming the result is stable and precise. Another is taking a single timing sample and treating it as authoritative, even though JIT warm-up and other runtime effects can dominate short measurements. Developers also benchmark tiny methods without protecting against dead-code elimination or optimizer artifacts, which makes the numbers look more certain than they really are.
Summary
- Use
System.nanoTime()for elapsed-time measurement. - Use
System.currentTimeMillis()for wall-clock timestamps, not precise duration timing. - Convert nanoseconds into milliseconds or other units for readable output.
- '
InstantandDurationare readable, but they are not a replacement for a monotonic timer in benchmarks.' - Use JMH when you need serious performance measurements rather than rough diagnostics.
Related reading
- How do I mock a REST template exchange?
- How do I modify proxy responses using asynchronous servlets?
- How do I monitor the computer''s CPU, memory, and disk usage in Java?
- How do I parse command line arguments in Java?
- How do I pass a class as a parameter in Java?
- How do I prevent the modification of a private field in a class?
- How do I print a double value without scientific notation using Java?
- How do I print my Java object without getting SomeType@2f92e0f4?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.