This tests how accurate the CodeTimer is compared to manually timing the code using System.nanoTime(). Run the main method to test.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| import static CodeTimer.*; import java.text.DecimalFormat; import java.util.*;
public class CodeTimerTest { public static void main(String[] args){ long testRunningTimeNanos = 11*NANOS_IN_A_SECOND; Random random = new Random(0); for (int i = 0; i < 1024; i++){ float randomAngle = random.nextFloat(); long t0 = System.nanoTime(); double sine = Math.sin(randomAngle); double cosine = Math.cos(randomAngle+1); double tan = Math.tan(randomAngle+2); double calc = Math.sqrt(Math.pow(Math.pow(sine, cosine), tan)); long t1 = System.nanoTime(); }
System.out.println(CodeTimer.class.getSimpleName()+": starting test 1."); long startNanos = System.nanoTime(); random = new Random(0); long nanoSelfTime = calibrateNanoTime(10000); System.out.println(CodeTimer.class.getSimpleName()+": System.nanoTime() selfTime == "+nanoSelfTime); long totalNanos = 0; double lastPrintOutNanos = System.nanoTime(); float maxSecondsBeforePrintOut = 5; long count = 0; while (true){ float randomAngle = random.nextFloat(); long t0 = System.nanoTime(); double sine = Math.sin(randomAngle); double cosine = Math.cos(randomAngle+1); double tan = Math.tan(randomAngle+2); double calc = Math.sqrt(Math.pow(Math.pow(sine, cosine), tan)); long t1 = System.nanoTime(); totalNanos += t1 - t0 - nanoSelfTime; count++; if (System.nanoTime() - lastPrintOutNanos > maxSecondsBeforePrintOut*NANOS_IN_A_SECOND){ double avTotalNanos = (double)totalNanos/(count); System.out.println(CodeTimer.class.getSimpleName()+": avTotalNanos == "+decimalFormat.format(avTotalNanos)); totalNanos = 0; count = 0; lastPrintOutNanos = System.nanoTime(); } if (System.nanoTime() - startNanos > testRunningTimeNanos){ break; } }
System.out.println(CodeTimer.class.getSimpleName()+": starting test 2."); startNanos = System.nanoTime(); random = new Random(1000); CodeTimer codeTimer = new CodeTimer(NANOS, false, NONE); System.out.println(CodeTimer.class.getSimpleName()+": CodeTimer.SELF_TIME == "+CodeTimer.SELF_TIME); while (true){ float randomAngle = random.nextFloat(); codeTimer.click(); double sine = Math.sin(randomAngle); double cosine = Math.cos(randomAngle+1); double tan = Math.tan(randomAngle+2); double calc = Math.sqrt(Math.pow(Math.pow(sine, cosine), tan)); codeTimer.lastClick();
if (System.nanoTime() - startNanos > testRunningTimeNanos){ break; } } System.out.println(CodeTimer.class.getSimpleName()+": finished.");
}
protected static long calibrateNanoTime(int numTests){ boolean print = false; for(int i=0; i<1024; i++){ System.nanoTime(); }
ArrayList<Long> selfTimeObservations = new ArrayList<Long>(numTests); for (int i = 0; i < numTests; i++){ long nanoSelfTime = -(System.nanoTime() - System.nanoTime()); if (print){ System.out.println(CodeTimer.class.getSimpleName()+": nanoSelfTime == "+nanoSelfTime); } selfTimeObservations.add(nanoSelfTime); } Collections.sort(selfTimeObservations); if (print){ for (int i = 0; i < selfTimeObservations.size(); i++){ System.out.println(CodeTimer.class.getSimpleName()+": selfTimeObservations.get(i) == "+selfTimeObservations.get(i)); } } for (int i = 0; i < (int)(numTests*0.05); i++){ selfTimeObservations.remove(0); } for (int i = 0; i < (int)(numTests*0.05); i++){ selfTimeObservations.remove(selfTimeObservations.size()-1); } if (print){ System.out.println(CodeTimer.class.getSimpleName()+": Slimmed list: selfTimeObservations.size() == "+selfTimeObservations.size()); for (int i = 0; i < selfTimeObservations.size(); i++){ System.out.println(CodeTimer.class.getSimpleName()+": selfTimeObservations.get(i) == "+selfTimeObservations.get(i)); } } long sumOfSelfTimes = 0; for (int i = 0; i < selfTimeObservations.size(); i++){ sumOfSelfTimes += selfTimeObservations.get(i); } long nanoSelfTime = sumOfSelfTimes/selfTimeObservations.size(); return nanoSelfTime; } } |
This was my output on Windows Vista, java 6 update 17:
CodeTimer: starting test 1.
CodeTimer: System.nanoTime() selfTime == 1381
CodeTimer: avTotalNanos == 1,276.9695 <--- 1st manual method measurement of average nanoseconds to do the calc's
CodeTimer: avTotalNanos == 1,253.9743 <--- 2nd manual method measurement of average nanoseconds to do the calc's
CodeTimer: starting test 2.
CodeTimer: CodeTimer.SELF_TIME == 1375
CodeTimer: avTotalNanos == 1,270.8229 <--- 1st convenience class measurement of average nanoseconds to do the calc's
CodeTimer: avTotalNanos == 1,260.9276 <--- 2nd convenience class measurement of average nanoseconds to do the calc's
CodeTimer: finished.
Which shows that the manual method and the convenience class give pretty similar results. Phew!