how to get last time interval since last split?
Asked Answered
H

2

6

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();
Handoff answered 24/2, 2016 at 18:21 Comment(3)
What exactly are you trying to do ?? Your For example and not example seem to be identical to me.Kingly
counting between splitsHandoff
I've opened a ticket for this issues.apache.org/jira/browse/LANG-1504Clathrate
B
3

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();
}
Bokbokhara answered 20/4, 2016 at 19:40 Comment(0)
I
1

Or you can use this:

private long stopWatchGap(StopWatch stopWatch) {
    try {
        stopWatch.stop();
        return stopWatch.getTime();
    } finally {
        stopWatch.reset();
        stopWatch.start();
    }
}

just to return gap value.

Ionosphere answered 22/4, 2020 at 6:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.