$ date ; java main <in >/dev/null ; date Thu Oct 29 13:15:14 EDT 2009 Thu Oct 29 13:15:19 EDT 2009 $
$ time java main/dev/null real 0m0.247s user 0m0.148s sys 0m0.060s $
Arithmetic mean: (23 + 20 + 18 + 23 + 28)/5 = 22.4 msec
Arithmetic mean: (1.2 + 4.6 + 0.9)/3 = 2.23
Geometric mean: 3√(1.2 × 4.6 × 0.9) = 1.7
Mean: 22.4, standard deviation: 3.4
Maximum - minimum = 28 - 18 = 10
Mean: 22.4, standard deviation: 8.5
Maximum - minimum = 38 - 13 = 25
int start = time() // execute code int elapsed = time() - start
Stopwatch sw sw.start() // execute code int elapsed = sw.stop()
public void Class Stopwatch start() { startTime = System.currentTimeMillis() } public int stop() { return (int) (System.currentTimeMillis() - startTime) } private long startTime
See the code.
Stopwatch sw = new Stopwatch() int perIteration = 0, n = 10 sw.start() for (int i = 0; i < n; i++) // whatever perIteration = sw.stop()/n
public void start() { times.add(System.currentTimeMillis()) } public void stop() { final int n = times.size() - 1 times.set(n, System.currentTimeMillis() - times.get(n)) } private Vector<Long> times
public double average() long total = 0 for (long t: times) total += t return ((double) total)/times.size() public double stddev() long sum = 0, sumSqr = 0 for (long t: times) sum += t sumSqr += t*t final int n = times.size() return Math.sqrt(( sumSqr - ((double) (sum*sum)/n))/(n - 1))
EpisodeTimer et = new EpisodeTimer() final int n = 10 for (int i = 0; i < n; i++) et.start() // whatever et.stop() System.out.println( et.average() + ", " + et.stddev())
This page last modified on 12 October 2009. |