You need to calculate the timing or elapsed time of your code execution, so you know how long a particular method or some block of code take to finish its execution. You can do this by capturing the start-time and the end-time using System.currentTimeMillis()
and find their differences. Another way is to use the StopWatch
class from the Apache Commons Lang library. The StopWatch
class can be found in the org.apache.commons.lang3.time
package.
The simplest steps to use the StopWatch
is to create an instance of the StopWatch
class, start the stopwatch by calling the start()
method. After the stopwatch is started you can execute the target method or block of code you want to watch and call the stop()
method to complete the timing session. To get the time of the stopwatch, you can call the getTime()
method.
Now, let’s see the code for the process described above.
package org.kodejava.commons.lang;
import org.apache.commons.lang3.time.StopWatch;
public class StopWatchExample {
public static void main(String[] args) {
StopWatchExample demo = new StopWatchExample();
demo.timingOne();
}
private void timingOne() {
// Create an instance of StopWatch.
StopWatch stopWatch = new StopWatch();
// Start the watch, do some task and stop the watch.
stopWatch.start();
doSomeTask(5000);
stopWatch.stop();
// Print out the total time of the watch
System.out.println("Time: " + stopWatch.getTime());
}
private void doSomeTask(long sleep) {
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Here is the output of the code above:
Time: 5000
Besides doing a simple timing calculation using the start()
and stop()
followed by the getTime()
methods, the StopWatch
class also provides methods for splitting the time, suspend and resuming the stopwatch. You can use the split()
, suspend()
and resume()
method respectively. To get the split time you can call the toSplitString()
method.
package org.kodejava.commons.lang;
import org.apache.commons.lang3.time.StopWatch;
public class StopWatchAnotherExample {
public static void main(String[] args) {
StopWatchAnotherExample demo = new StopWatchAnotherExample();
demo.timingTwo();
}
private void timingTwo() {
// Create an instance of StopWatch and start the stopwatch.
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// Do some task and split the stopwatch time.
doSomeTask(3000);
stopWatch.split();
System.out.println("Split 1: " + stopWatch.toSplitString());
// Suspend the stopwatch and resume the stopwatch.
stopWatch.suspend();
doSomeTask(4000);
stopWatch.resume();
// Do some task and split the stopwatch time.
doSomeTask(2500);
stopWatch.split();
System.out.println("Split 2: " + stopWatch.toSplitString());
// Do some task and split the stopwatch time.
doSomeTask(1000);
stopWatch.split();
System.out.println("Split 3: " + stopWatch.toSplitString());
// Stop the stopwatch and the total execution time.
stopWatch.stop();
System.out.println("Time: " + stopWatch.getTime());
}
private void doSomeTask(long sleep) {
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
The code snippet above will output something like this:
Split 1: 00:00:03.004
Split 2: 00:00:05.518
Split 3: 00:00:06.522
Time: 6522
Another method that you can find in the StopWatch
class is the getStartTime()
which will return the stopwatch start time. The reset()
method will reset the stopwatch. To remove a split, you can call the unsplit()
method.
Maven Dependencies
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>