1 package org.codehaus.nanning.samples;
2
3 public class StopWatch {
4 ///CLOVER:OFF
5
6 public static final double MILLIS_PER_SECOND = 1000;
7 public static final int BYTES_PER_K = 1024;
8
9 private long startMemory;
10 private long startTime;
11 private long time;
12 private long memory;
13 private boolean stopped = false;
14
15 public StopWatch() {
16 this(false);
17 }
18
19 public StopWatch(boolean doGC) {
20 if (doGC) {
21 System.gc();
22 System.gc();
23 System.gc();
24 }
25 startMemory = Runtime.getRuntime().freeMemory();
26 startTime = System.currentTimeMillis();
27 }
28
29 public void stop() {
30 stopped = true;
31 time = System.currentTimeMillis() - startTime;
32 memory = startMemory - Runtime.getRuntime().freeMemory();
33 }
34
35 public double getTimeSpent() {
36 return time;
37 }
38
39 public double getMemoryUsed() {
40 return memory;
41 }
42
43 public double getTimeSpent(int numberOfIterations) {
44 assert stopped : "you need to invoke stop() first";
45 return getTimeSpent() / (double) numberOfIterations;
46 }
47
48 public double getMemoryUsed(int numberOfIterations) {
49 assert stopped : "you need to invoke stop() first";
50 return getMemoryUsed() / (double) numberOfIterations;
51 }
52
53 public String getTimeSpentSeconds() {
54 assert stopped : "you need to invoke stop() first";
55 return getTimeSpent() / MILLIS_PER_SECOND + "s";
56 }
57
58 public String getMemoryUsedKs() {
59 assert stopped : "you need to invoke stop() first";
60 return getMemoryUsed() / BYTES_PER_K + "k";
61 }
62 ///CLOVER:ON
63 }
This page was automatically generated by Maven