1111 */
1212public class BenchmarkTimer {
1313
14+ private static final int WARMUP_CALLS = 100 ;
15+
1416 public static class BenchmarkMultiTimer extends BenchmarkTimer {
1517 ConcurrentHashMap <Integer ,Long > timeAtResume = new ConcurrentHashMap <>();
1618 ConcurrentHashMap <Integer ,Boolean > isPaused = new ConcurrentHashMap <>();
@@ -23,8 +25,8 @@ public void resume(int thread) {
2325 }
2426 public void pause (int thread ) {
2527 if (!isPaused .get (thread )) {
26- numberOfCalls .incrementAndGet ();
27- totalTime .addAndGet (System .nanoTime () - timeAtResume .get (thread ));
28+ if ( numberOfCalls .incrementAndGet () > WARMUP_CALLS )
29+ totalTime .addAndGet (System .nanoTime () - timeAtResume .get (thread ));
2830 isPaused .put (thread , true );
2931 }
3032 }
@@ -62,8 +64,8 @@ public void resume(){
6264
6365 public void pause (){
6466 if (isPaused ==false ){
65- totalTime . addAndGet ( System . nanoTime () - timeAtResume );
66- numberOfCalls . incrementAndGet ( );
67+ if ( numberOfCalls . incrementAndGet () > WARMUP_CALLS )
68+ totalTime . addAndGet ( System . nanoTime () - timeAtResume );
6769 isPaused = true ;
6870 }
6971 }
@@ -84,13 +86,13 @@ public double getSeconds(){
8486 }
8587
8688 public double getMillisecondsPerCall () {
87- return numberOfCalls .get () > 0 ? getMilliseconds () / numberOfCalls .get () : 0 ;
89+ return numberOfCalls .get () > WARMUP_CALLS ? getMilliseconds () / ( numberOfCalls .get () - WARMUP_CALLS ) : 0 ;
8890 }
8991
9092 @ Override
9193 public String toString () {
9294 return String .format ("%-15s : #Calls %12d, Total = %12.2f sec, Unit = %12.3f msec" ,
93- getName (), numberOfCalls .get (), getSeconds (), getMillisecondsPerCall ());
95+ getName (), numberOfCalls .get ()- WARMUP_CALLS , getSeconds (), getMillisecondsPerCall ());
9496 }
9597
9698}
0 commit comments