Search code examples
javaloggingtimesplitstopwatch

how to get last time interval since last split?


I want to run a stop watch form the beginning of my program

and log the time split of some intervals.

Which StopWatch should I prefer?

import com.google.common.base.Stopwatch;

org.apache.commons.lang.time.StopWatch

For example:

Start -----> split 1 (1:00 min from split 0) ----> split 2 (0:30 from split 1) -->  split 3 (0:35 from split 2)

and not

Start -----> split 1 (1:00 min from start) ----> split 2 (1:30 from from start) ----> split 3 (2:05 from from start) 

Is there a more elegant way to do so than this?

stopWatch.split();
stopWatch.getSplitTime() - lastSpiltTime;
last splitTime = stopWatch.getSplitTime();

Solution

  • Unfortunately, this is the only way. I had the same need, but Apache StopWatch does not have native support for this.

    I did it a slightly different way:

    private void lapWatchAndLog( StopWatch watch, String messageForLap ) {
    
        watch.stop();
        LOGGER.info( String.format( "Time: [%s] %s", watch.getTime(), messageForLap ) );
        watch.reset();
        watch.start();
    }