How to Use Guava Stopwatch

The Stopwatch class in Google’s Guava library is a powerful utility for measuring elapsed time in our applications. It simplifies the process of tracking how long operations take, which can be useful for performance monitoring, debugging, and optimization.

Adding Guava to our Project

If you’re using Maven, add the following dependency to the pom.xml file:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version> <!-- Check for the latest version -->
</dependency>

For Gradle, add this to the build.gradle:

implementation 'com.google.guava:guava:31.0.1-jre' // Check for the latest version

Creating and Starting a Stopwatch

To start using Stopwatch, we need to create an instance and start it. Here’s a simple example:

        Stopwatch stopwatch = Stopwatch.createStarted();
        // Simulate the first task
        Thread.sleep(1000);
        System.out.println("Elapsed time when the first task finishes: " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " milliseconds");
        // Simulate the second task
        Thread.sleep(2000);
        System.out.println("Elapsed time when the second task finishes: " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " milliseconds");
        // Total elapsed time
        System.out.println("Total elapsed time: " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " milliseconds");

The createStarted() method create and start a stopwatch for us. The output for the above codes:

elapsed time when the first task finishes: 1005 milliseconds
elapsed time when the second task finishes: 3020 milliseconds
Total elapsed time: 3021 milliseconds

In the above codes, the time taken by System.out.println method is also counted in the result, if we want to make the result more accurate, we can explictly call start and stop method before and after each task.

        Stopwatch stopwatch = Stopwatch.createStarted();
        // Simulate the first task
        Thread.sleep(1000);
        stopwatch.stop();
        System.out.println("Elapsed time when the first task finishes: " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " milliseconds");
        // Simulate the second task
        stopwatch.start();
        Thread.sleep(2000);
        stopwatch.stop();
        System.out.println("Elapsed time when the second task finishes: " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " milliseconds");
        // Total elapsed time
        System.out.println("Total elapsed time: " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " milliseconds");

The output for the above codes:

Elapsed time when the first task finishes: 1005 milliseconds
Elapsed time when the second task finishes: 3010 milliseconds
Total elapsed time: 3010 milliseconds

Measuring Time Taken by the every Task

We can measure elapse time for multiple tasks by reseting and starting the stopwatch again:

        Stopwatch stopwatch = Stopwatch.createStarted();
        // First task
        Thread.sleep(1000);
        System.out.println("time taken by the first task: " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " milliseconds");

        stopwatch.reset().start();
        // Second task
        Thread.sleep(2000);
        System.out.println("time taken by the second task: " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " milliseconds");

The output for the above codes:

time taken by the first task: 1001 milliseconds
time taken by the second task: 2005 milliseconds