Many things in programming sound like they should be easy, but are quite hard. Calculating elapsed time in Java is one of those. How hard could that be? As it turns out, it can be tricky.
For starters, we have the fact that time itself is a tricky concept. For a quick example, remember that many places around the world observe Daylight Savings Time. Failing to take DST into account when you should, can and often does result in incorrect calculations.
Then, we have the problem that, in Java, there are many ways of performing this calculation. You have different alternatives brought to you by the Java API itselfâ€”and donâ€™t forget that Java 8 brought us an entirely new date and time API, based on the popular Joda-time open-source project. Additionally, third-party libraries add their own options to the mix.
In this post, weâ€™ll offer some guidance so you can decide the most appropriate option to use when calculating elapsed time in Java. Weâ€™ll start by covering the different types of elapsed time calculation one might need to perform. Then weâ€™ll quickly turn our focus to the alternatives themselves, explaining the strengths and weaknesses of each one. By the end of the post, youâ€™ll be aware of the main ways in which you can perform this calculation, and youâ€™ll be able to make an informed decision about the best alternative for your scenario. Letâ€™s begin!
Java Elapsed Time Calculation: Not a Single Thing
There are several different ways of calculating elapsed time in Java. In order to make the right call for your scenario, there are several factors you must consider. One of them refers to the type of elapsed time calculation.
When it comes to the motivations behind calculating elapsed time, we can broadly categorize them into two main groups. The first category is what I call the â€œbusiness logicâ€ motivation. This is when you have to calculate the elapsed time of some event that is relevant to your applicationâ€™s core business or domain. A good example would be recording the time a worker clocks in and out and then calculating the amount of time worked.
The second category is what I like to call â€œmetaâ€ motivation. This means calculating elapsed time for some reason thatâ€™s not related to the domain of your application, but rather, refers to some concern about the development itself. The classic example here would be benchmarking a function for performance measurement purposes.
Why is this distinction important? For now, understand that itâ€™s going to be crucial to decide which one of several options is the right one to calculate elapsed time. Weâ€™ll cover that in more detail in the sections to come.
Calculating Elapsed Time in Java in All Shapes and Sizes
Without further ado, letâ€™s now cover some of the main ways to perform a calculation of elapsed time in Java.
The Quick, Easy, and Often Incorrect Way: System.currentTimeMillis()
We start our list of ways to measure elapsed time in Java with a solution thatâ€™s easy but often incorrect. First, weâ€™ll show a quick example of how to perform the calculation, and after that weâ€™ll explain the code:
long start = System.currentTimeMillis(); // some time passes long end = System.currentTimeMillis(); long elapsedTime = end - start;
In the example above, weâ€™re using the â€œSystem.currentTimeMillis()â€ static method. The method returns a long value, which refers to the number of milliseconds since January 1st, 1970, in UTC. So, why is this solution problematic?
The results you get from the difference may be inaccurate because the method measures what we call â€œwall-clock time.â€ That means it can change for a variety of reasons, including changes in the system clock or even leap-seconds.
So, in short, you should avoid using the currentTimeMillis() method for calculating elapsed time if you need high precision.
The Also Quick, Also Easy and More Likely to Be Correct Way: System.nanoTime()
Letâ€™s now see another way of calculating elapsed time. Weâ€™ll first notice the example and then comment on it.
long start = System.nanoTime(); // some time passes long end = System.nanoTime(); long elapsedTime = end - start;
As you can see, the code looks a lot like the code in the previous example. The only difference is that now weâ€™re using the â€œnanoTime()â€ method instead of â€œcurrentTimeMillis().â€ So, whatâ€™s the difference between the two methods?
The first notable difference is that â€œnanoTime(),â€ as its name suggests, returns its result in nanoseconds. Per the documentation, we can see that the source of its return value is the high-resolution time source of the JVM (Java Virtual Machine.)
The documentation also stresses some other important facts. The first of them is that you can only use this method to calculate elapsed time. In other words, the value returned by the method isnâ€™t meaningful, since it has no relation to any time system.
Another important thing to bear in mind when using this method is that, while it provides nanosecond precision, it doesnâ€™t necessarily offer nanosecond resolution. â€œResolutionâ€ here means the frequency with which the value is updated.
Finally, bear in mind that the method isnâ€™t thread-safe.
The StopWatch Class
Weâ€™ll now turn our focus to libraries, by covering the StopWatch class provided by the Apache Commons Lang API. Letâ€™s see a quick example:
StopWatch watch = new StopWatch(); watch.start(); // call to the methods you want to benchmark watch.stop(); long result = watch.getTime();
As you can see from the code above, the usage of the StopWatch is very simple. You start the timing by calling the â€œstart()â€ method. Then you do whatever tasks you need to perform. After that, youâ€™re ready to call â€œstop()â€ and then â€œgetTime()â€ which will return the elapsed time in milliseconds. Keep in mind that this class presents temporal coupling. That means you canâ€™t, for instance, stop a clock that you havenâ€™t started or start a clock that is already running. Attempting to do so will result in an exception being thrown.
Post-Java 8 Alternatives
Java 8 brought us the new java.time API, which introduced several new types representing important time concepts. Youâ€™ll now see how to calculate elapsed time using two of the new java.time types: the Instant and the Duration classes. Take a look at the following example:
Instant start = Instant.now(); // time passes Instant end = Instant.now(); Duration timeElapsed = Duration.between(start, end);
The first new thing we notice is the Instant class, which is immutable and thread-safe. This new type represents a single, instantaneous point in the timeline, and, as such, you can use it to calculate elapsed time. But to do that, weâ€™d need a new class yet, and thatâ€™s where Duration comes in to help.
Duration, like Instant, is an immutable and thread-safe class. According to the documentation, it represents an amount of time that is time-based, such as â€œ4 hoursâ€ or â€œ36 seconds.â€
By calling the â€œbetweenâ€ method and providing it two instances of the Instant class, weâ€™re able to calculate a duration. Afterward, you can use one of Durationâ€™s methodsâ€”such as â€œtoDays(),â€ â€œtoHours(),â€ â€œtoMinutes(),â€ and so onâ€”to convert the duration to the unit thatâ€™s more appropriate for your needs.
Learn About Time. Itâ€™s About Time!
In this post, weâ€™ve covered some of the main alternatives for calculating elapsed time in Java. As you can see, there are alternatives that cater to different needs. If you need the highest possible precision, you wonâ€™t be using â€œcurrentTimeMillis(),â€ for instance. On the other hand, if you need thread-safety, you can discard â€œnanoTime().â€
Also, keep in mind that we havenâ€™t presented all of the possible alternatives to perform this calculation. For instance, we didnâ€™t mention this other StopWatch class that is provided by the Guava API.