DEV Community

Hyesung Lee
Hyesung Lee

Posted on

Measuring Code Execution Time Using Stopwatch

You might have used the System.out.println(end - start + "ms elapsed") through System.currentTimeMillis() to measure the execution time of a piece of code.

If you have a small number of code points to measure, it can be quite appropriate to do this.

But when we have a lot of code points to measure, this makes us difficult.

If you want to see the result with a meaningful name and how much of the total time it takes, you have to add cumbersome code to do it. like:

long start = System.currentTimeMillis();

long initStart = System.currentTimeMillis();
// initialization
long initEnd = System.currentTimeMillis();

long processingStart = System.currentTimeMillis();
// processing
long processingEnd = System.currentTimeMillis();

long end = System.currentTimeMillis();

System.out.println("initialization: " + (initEnd - initStart) + "ms");
System.out.println("processing: " + (processingEnd - processingStart) + "ms");
System.out.println("total: " + (end - start) + "ms");
Enter fullscreen mode Exit fullscreen mode

It's Time to Use Stopwatch

Stopwatch is a library that helps you measure the execution time of a piece of code with a meaningful name and how much of the total time it takes.

We can simply use it like this:

Stopwatch stopwatch = new Stopwatch();

stopwatch.start("initialization");
// ...
stopwatch.stop();

stopwatch.start("processing");
// ...
stopwatch.stop();

stopwatch.start("rendering");
// ...
stopwatch.stop();

stopwatch.print();
Enter fullscreen mode Exit fullscreen mode

The stopwatch.print() will give you the result like this:

|           name |     % |      ms |      s |
|----------------|-------|---------|--------|
| initialization | 59.5% | 1,234ms | 1.234s |
|     processing | 40.0% |   830ms | 0.830s |
|      rendering |  0.5% |    10ms | 0.010s |
|                |       |         |        |
|          total |  100% | 2,074ms | 2.074s |
Enter fullscreen mode Exit fullscreen mode

One More Thing

The Stopwatch can be paused and resumed to remove the time between code points where you do not want to measure. like this:

stopwatch.start("rendering");
// prepare rendering
stopwatch.pause();
// load font stuff
stopwatch.resume();
// render
stopwatch.stop();
Enter fullscreen mode Exit fullscreen mode

Conclusion

Stopwatch can remove chores from your code. If you have a lot of code points to measure, or you want to see the result with a meaningful name and how much of the total time it takes, you can use it.

Thank you for reading this article. If you like Stopwatch, give it a 🌟 on GitHub.

GitHub logo silentsoft / stopwatch

⚑ Stopwatch for get rid of chore in your code.

Stopwatch

Maven Central Build Status Quality Gate Status Coverage Hits

Stopwatch for get rid of chore in your code.

Usage

Stopwatch stopwatch = new Stopwatch();

stopwatch.start("initialization");
// ...
stopwatch.stop();

stopwatch.start("processing");
// ...
stopwatch.stop();

stopwatch.start("rendering");
// ...
stopwatch.stop();

stopwatch.print();
Enter fullscreen mode Exit fullscreen mode

Result

|           name |     % |      ms |      s |
|----------------|-------|---------|--------|
| initialization | 59.5% | 1,234ms | 1.234s |
|     processing | 40.0% |   830ms | 0.830s |
|      rendering |  0.5% |    10ms | 0.010s |
|                |       |         |        |
|          total |  100% | 2,074ms | 2.074s |

Maven Central

<dependency>
    <groupId>org.silentsoft</groupId>
    <artifactId>stopwatch</artifactId>
    <version>2.2.1</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like…

Top comments (0)