Measuring Java Code Execution Time

Learn three ways to measure the execution time of Java code.

Overview

When coding, you may need to measure execution time because latency occurs or because you want to know which section is slow. You may also want to measure the speed of an algorithm or logic you wrote.

For these and other reasons, this article introduces three ways to measure the execution time of a specific section of code.

System.currentTimeMillis()

You can use Java’s built-in System.currentTimeMillis() method to output the current time in milliseconds.
This value is the time difference from January 1, 1970 UTC to the current time, expressed in milliseconds.

By using this method at the start and end of the section you want to measure and calculating the difference between the two times, you can get the time difference in ms.

long beforeTime = System.currentTimeMillis(); // Get time before code execution
        
// Code to measure...
        
long afterTime = System.currentTimeMillis(); // Get time after code execution
long diffTime = afterTime - beforeTime; // Execution time difference
System.out.println("실행 시간(ms): " + diffTime); // Seconds conversion

If you want to convert ms to seconds, divide by 1000. If you also want to convert seconds to minutes, divide by 60 once more.

The usage example is as follows.

package com.devkuma.basic.executetime;

public class ExecuteTime {
    public static void main(String[] args) {
        long beforeTime = System.currentTimeMillis(); // Get time before code execution

        int sum = 0;
        for (int i = 0; i < 100000; i++) {
            for (int j = 0; j < 50000; j++) {
                sum += i * j;
            }
        }
        System.out.println(sum);

        long afterTime = System.currentTimeMillis(); // Get time after code execution
        long diffTimeSec = (afterTime - beforeTime) / 1000; // Execution time difference
        System.out.println("실행 시간(sec): " + diffTimeSec); // Seconds conversion
    }
}

Result:

실행 시간(sec) : 1

Instant

Instant is a class added to the java.time package in Java 8, and it represents elapsed time from EPOCH in nanoseconds.

Instant beforeTime = Instant.now();  // Get time before code execution

// Code to measure...

Instant afterTime = Instant.now();
long diffTime = Duration.between(beforeTime, afterTime).toMillis(); // Execution time difference
System.out.println("실행 시간(ms): " + diffTime);

StopWatch - Spring

Another method is to use StopWatch included in the Spring Framework.

To use Spring’s StopWatch, you must first add the dependency with Gradle or Maven.

gradle

implementation 'org.springframework:spring-core:6.0.7'

maven

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>6.0.7</version>
</dependency>
StopWatch stopWatch = new StopWatch();
stopWatch.start();

// Code to measure...

stopWatch.stop();
System.out.println("실행 시간(ms): " + stopWatch.getTotalTimeMillis());
System.out.println(stopWatch.prettyPrint());