How to log a method's execution time exactly in milliseconds?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
You cannot log a method's execution time exactly in milliseconds in the strict mathematical sense. Operating-system scheduling, clock resolution, garbage collection, and measurement overhead all introduce noise, so the real goal is to measure elapsed time accurately enough for engineering decisions.
Use a Monotonic Clock, Not Wall Time
For duration measurement, use a monotonic timer. Wall-clock APIs such as currentTimeMillis can jump due to clock synchronization and are better suited for timestamps than elapsed-time measurement.
In Java, System.nanoTime() is the standard choice for timing code execution.
This gives you millisecond output while still measuring with nanosecond-resolution hardware when available.
Why "Exactly" Is the Wrong Requirement
Even with a high-resolution timer, the measured value includes more than your method body. It includes timer-call overhead, thread scheduling gaps, JIT warmup effects, cache behavior, and sometimes work done by the runtime on behalf of your method.
That does not make the measurement useless. It means the measurement is observational, not perfect. For logging and troubleshooting, that is usually enough. For serious benchmarking, you need repeated runs, warmup, and statistical analysis rather than one log line.
Logging in Milliseconds Versus Measuring in Milliseconds
Measure with the finest practical timer, then format in milliseconds for readability. Logging only integer milliseconds throws away detail and can make fast methods appear to take zero time.
For example, 0.173 ms is much more informative than 0 ms. If your logging policy requires integers, round at the very end rather than measuring with a coarse clock.
It is also useful to log the method label, input size, or request identifier beside the duration. A timing number without context is hard to compare later, especially when the same method runs on very different workloads.
When Logging Is Not Enough
Timing one method call in production logs is good for spotting slow outliers, but it is not a substitute for a profiler. If you are investigating micro-optimizations, benchmark with a proper harness. If you are diagnosing end-user latency, include surrounding context such as input size, request ID, and downstream service timings.
Good measurement depends on asking the right question. A single timing line is excellent for operational visibility and weak for performance science.
Common Pitfalls
- Using wall-clock time for durations can produce misleading results if the system clock changes.
- Logging integer milliseconds for very fast methods hides useful sub-millisecond variation.
- Treating one measurement as exact ignores scheduler noise, JIT warmup, and runtime overhead.
- Benchmarking by adding log statements inside tight loops changes the thing you are trying to measure.
- Comparing timings across machines without noting workload and environment usually leads to bad conclusions.
Summary
- Exact millisecond timing is not realistic, but reliable elapsed-time measurement is.
- Use a monotonic clock such as
System.nanoTime()for durations. - Measure with fine resolution and format the result in milliseconds for humans.
- Add enough context to timing logs so the number can be interpreted later.
- One log line is useful for diagnostics, but not a substitute for proper benchmarking.
Related reading
- How to log all active properties of a spring boot application before the beans instantiation?
- How to log all Cognito User details in API Gateway Cloudwatch
- How to log all Rabbit MQ messages?
- How to log formatted message, object array, exception?
- How to lose margin/padding in UITextView
- How to make async messaging fast and reliable in a sync environment?
- How to log Keras loss output to a file
- How to log request and response bodies in Spring WebFlux

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.