我想在程序开始时运行秒表
并记录某些间隔的时间分割。
我更喜欢哪种秒表?
import com.google.common.base.Stopwatch;
org.apache.commons.lang.time.StopWatch
例如:
Start -----> split 1 (1:00 min from split 0) ----> split 2 (0:30 from split 1) --> split 3 (0:35 from split 2)
而不是
Start -----> split 1 (1:00 min from start) ----> split 2 (0:30 from from start) ----> split 3 (0:35 from from start)
这样做比这更优雅吗?
stopWatch.split();
stopWatch.getSplitTime() - lastSpiltTime;
last splitTime = stopWatch.getSplitTime();
答案 0 :(得分:2)
不幸的是,这是唯一的方法。我有同样的需求,但Apache StopWatch没有本机支持。
我的方式略有不同:
private void lapWatchAndLog( StopWatch watch, String messageForLap ) {
watch.stop();
LOGGER.info( String.format( "Time: [%s] %s", watch.getTime(), messageForLap ) );
watch.reset();
watch.start();
}
答案 1 :(得分:0)
或者您可以使用此:
private long stopWatchGap(StopWatch stopWatch) {
try {
stopWatch.stop();
return stopWatch.getTime();
} finally {
stopWatch.reset();
stopWatch.start();
}
}
只是返回间隙值。