Quick Answer: Measuring execution time using wall-clock functions like Date.now() or System.currentTimeMillis() can yield negative durations if your server synchronizes its clock mid-operation. To measure elapsed code performance accurately, always use monotonic clock APIs like performance.now() in JavaScript or System.nanoTime() in Java, which strictly count forward from an arbitrary baseline.
Imagine profiling an asynchronous task in your API that typically takes 200 milliseconds to complete, only for your logging framework to report a duration of -800ms . While a negative duration sounds like a physics violation, I often see this exact issue trip up engineers who rely on wall-clock time for benchmarking.
When I time execution intervals, relying on time-of-day calls exposes metrics to background operating system updates.…