Backend Development 5 min read

Using Spring's StopWatch to Measure Code Execution Time

This article explains how to use Spring's built‑in StopWatch utility to record the start and end times of multiple code sections, generate a clear performance summary, and compares it with similar tools from Apache Commons and Guava.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Using Spring's StopWatch to Measure Code Execution Time

Many developers encounter long response times after deploying a feature and need to locate the bottleneck.

Common practice is to embed start and end timestamps in many places, subtract them to get the duration, and print the result. While simple, this approach pollutes the codebase with low‑level timing statements.

Spring provides a lightweight utility class org.springframework.util.StopWatch that can record multiple named tasks and produce a consolidated report, making the analysis more intuitive.

long start1 = System.currentTimeMillis();
// simulate business logic
Thread.sleep(300);
long end1 = System.currentTimeMillis();

long start2 = System.currentTimeMillis();
// simulate business logic
Thread.sleep(730);
long end2 = System.currentTimeMillis();

System.out.println("Execution 1, time:" + (end1 - start1));
System.out.println("Execution 2, time:" + (end2 - start2));

The Maven dependency required for the StopWatch class is:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.2.9.RELEASE</version>
</dependency>

Typical usage example:

StopWatch sw = new StopWatch();
sw.start("Task 1");
// simulate business logic
Thread.sleep(300);
sw.stop();

sw.start("Task 2");
// simulate business logic
Thread.sleep(730);
sw.stop();

System.out.println(sw.prettyPrint());

Running the above code produces output similar to:

StopWatch '' : running time (millis) = 1033
-----------------------------------------
ms   %   Task name
-----------------------------------------
00303  29%  Task 1
00730  71%  Task 2

Advantages of Spring's StopWatch:

Provided by the Spring framework, ready to use without additional libraries.

Each task can be given a name, making the report more readable.

Aggregates data and shows each task’s duration and its percentage of total time.

Disadvantages:

A single StopWatch instance cannot run multiple tasks concurrently; a new task can only be started after the previous one is stopped.

The API is intrusive, requiring explicit start/stop calls in the code.

Similar utilities are available in other libraries, such as org.apache.commons.lang.time.StopWatch from Apache Commons Lang and com.google.common.base.Stopwatch from Guava, which offer comparable functionality with slightly different APIs.

The source code for the example project is hosted at https://github.com/aalansehaiyang/spring-boot-bulking (module: spring-boot-bulking-tool).

For more advanced system diagnostics, Alibaba open‑sources the Arthas tool, which provides online analysis of performance bottlenecks.

performanceProfilingStopWatch
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.