Six Common Ways to Measure Execution Time in Java
This article introduces six practical methods for measuring Java code execution time, covering built‑in System.currentTimeMillis, System.nanoTime, new Date, Spring’s StopWatch, Apache Commons‑lang3 StopWatch, and Guava’s Stopwatch, and explains their underlying principles and usage examples.
Method 1: System.currentTimeMillis
Uses the built‑in System.currentTimeMillis() to measure elapsed time in milliseconds. Example code demonstrates capturing start and end timestamps around a Thread.sleep(1000) call and printing the duration.
public class TimeIntervalTest {
public static void main(String[] args) throws InterruptedException {
// 开始时间
long stime = System.currentTimeMillis();
// 执行时间(1s)
Thread.sleep(1000);
// 结束时间
long etime = System.currentTimeMillis();
// 计算执行时间
System.out.printf("执行时长:%d 毫秒.", (etime - stime));
}
}Method 2: System.nanoTime
Uses System.nanoTime() for nanosecond precision. The same pattern as above is applied, printing the duration in nanoseconds.
public class TimeIntervalTest {
public static void main(String[] args) throws InterruptedException {
long stime = System.nanoTime();
Thread.sleep(1000);
long etime = System.nanoTime();
System.out.printf("执行时长:%d 纳秒.", (etime - stime));
}
}Method 3: new Date
Creates two java.util.Date objects before and after the code block and computes the difference via getTime() to obtain milliseconds.
import java.util.Date;
public class TimeIntervalTest {
public static void main(String[] args) throws InterruptedException {
Date sdate = new Date();
Thread.sleep(1000);
Date edate = new Date();
System.out.printf("执行时长:%d 毫秒.", (edate.getTime() - sdate.getTime()));
}
}Method 4: Spring StopWatch
Leverages org.springframework.util.StopWatch to start, stop, and retrieve total time in seconds, milliseconds, and nanoseconds.
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Thread.sleep(1000);
stopWatch.stop();
System.out.printf("执行时长:%d 秒.%n", stopWatch.getTotalTimeSeconds());
System.out.printf("执行时长:%d 毫秒.%n", stopWatch.getTotalTimeMillis());
System.out.printf("执行时长:%d 纳秒.%n", stopWatch.getTotalTimeNanos());Method 5: Apache Commons‑lang3 StopWatch
Adds the commons-lang3 dependency and uses its StopWatch to measure time, retrieving results via TimeUnit constants.
import org.apache.commons.lang3.time.StopWatch;
import java.util.concurrent.TimeUnit;
public class TimeIntervalTest {
public static void main(String[] args) throws InterruptedException {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Thread.sleep(1000);
stopWatch.stop();
System.out.println("执行时长:" + stopWatch.getTime(TimeUnit.SECONDS) + " 秒.");
System.out.println("执行时长:" + stopWatch.getTime(TimeUnit.MILLISECONDS) + " 毫秒.");
System.out.println("执行时长:" + stopWatch.getTime(TimeUnit.NANOSECONDS) + " 纳秒.");
}
}Method 6: Guava Stopwatch
Uses Guava’s Stopwatch (created with Stopwatch.createStarted() ) to measure elapsed time in various units and demonstrates resetting and reusing the timer.
import com.google.common.base.Stopwatch;
import java.util.concurrent.TimeUnit;
public class TimeIntervalTest {
public static void main(String[] args) throws InterruptedException {
Stopwatch stopwatch = Stopwatch.createStarted();
Thread.sleep(1000);
stopwatch.stop();
System.out.printf("执行时长:%d 毫秒. %n", stopwatch.elapsed(TimeUnit.MILLISECONDS));
stopwatch.reset();
stopwatch.start();
Thread.sleep(2000);
stopwatch.stop();
System.out.printf("执行时长:%d 秒. %n", stopwatch.elapsed(TimeUnit.SECONDS));
}
}Principle Analysis
Both Spring’s and Guava’s StopWatch implementations ultimately rely on System.nanoTime() to capture start and end timestamps, then compute the elapsed interval.
Conclusion
For simple timing, the built‑in methods System.currentTimeMillis() , System.nanoTime() , or new Date() are sufficient. When using frameworks, Spring’s StopWatch , Apache Commons‑lang3’s StopWatch , or Guava’s Stopwatch provide convenient APIs for more detailed measurements.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.