Practice ยท 1 of 2
Timing that cannot lie
1. static long timedSum(int n) โ time a loop summing 1..n with nanoTime; return
the elapsed NANOS. Verify the sum equals (long) n * (n + 1) / 2 and throw
IllegalStateException if it does not (defeat dead-code elimination by consuming
the result).
2. static long warmMedianSum(int n, int rounds) โ for each of rounds rounds:
time the same sum, convert to microseconds, collect; return the MEDIAN of the
rounds (sort, take the middle). Warmup is inside: run the sum 2_000 times
untimed before measuring.
3. static boolean nanoIsMonotonic() โ read nanoTime 1_000 times; return true iff
no read was less than its predecessor (strictly: never a NEGATIVE step).
Difficulty: advanced
Press Submit to check your solution.
Back to lesson: Practice: Measurement drills