
JVM timing options
For as long as I have been coding in Java, we have had requirements to measure the execution time of blocks of code. While the current good practice is to use OpenTelemetry's traces, not every company has reached this stage yet. Plus, some of the alternatives are OpenTelemetry-compatible. Let's see them in order. The basic option The basic option is what we have been doing for ages, and what the other options rely on anyway. It's based on the following API: System.currentTimeMillis() . Usage is pretty straightforward: long start = System . currentTimeMillis (); // Execute code long end = System . currentTimeMillis (); System . out . println ( "Code executed in " + ( end - start ) + " milliseconds" ); The Object-Oriented alternative If you are an OOP developer, then you'd rather encapsulate state. Here's a draft of such encapsulation: class Timer { private long startTime ; public void start () { startTime = System . currentTimeMillis (); } public long stop () { return System . currentTi
Continue reading on Dev.to
Opens in a new tab



