Last Update: 27.03.2018. By Jens in Developers Life | Learning | Newsletter
Sometimes, we have to measure the execution time of certain code blocks. The traditional way would be to capture start and stop time with System.nanoTime() and print out the difference.
Works, but gets messy when you try to capture more than one time.
Spring provides a great helper for that: StopWatch
Just create an instance of it and use the start and stop methods for time capturing. In the end, call prettyPrint on it, which will provide a nice looking execution-time table.
Sample:
*StopWatch watch = new StopWatch();
watch.start();
// do stuff
watch.stop();
System.out.println(watch.prettyPrint());*
The start method does also have a variant accepting a name for the measuring block. This is useful when using multiple start-stop blocks in the same method.