StopWatch.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package util;
  2. import java.lang.management.ManagementFactory;
  3. import java.lang.management.ThreadMXBean;
  4. import exceptions.StopWatchException;
  5. public class StopWatch {
  6. private String task;
  7. private long startWC;
  8. private long startCPU;
  9. public long elapsedWC;
  10. public long elapsedCPU;
  11. private boolean isOn;
  12. public StopWatch() {
  13. task = "";
  14. startWC = 0;
  15. startCPU = 0;
  16. elapsedWC = 0;
  17. elapsedCPU = 0;
  18. isOn = false;
  19. }
  20. public StopWatch(String t) {
  21. task = t;
  22. startWC = 0;
  23. startCPU = 0;
  24. elapsedWC = 0;
  25. elapsedCPU = 0;
  26. isOn = false;
  27. }
  28. public void reset() {
  29. if (isOn) {
  30. try {
  31. throw new StopWatchException("StopWatch is still running");
  32. } catch (StopWatchException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. startWC = 0;
  37. startCPU = 0;
  38. elapsedWC = 0;
  39. elapsedCPU = 0;
  40. }
  41. public void start() {
  42. if (isOn) {
  43. try {
  44. throw new StopWatchException("StopWatch is already running");
  45. } catch (StopWatchException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. isOn = true;
  50. startWC = System.nanoTime();
  51. startCPU = getCPUTime();
  52. }
  53. public void stop() {
  54. if (!isOn) {
  55. try {
  56. throw new StopWatchException("StopWatch is not running");
  57. } catch (StopWatchException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. isOn = false;
  62. elapsedCPU += getCPUTime() - startCPU;
  63. elapsedWC += System.nanoTime() - startWC;
  64. }
  65. private long getCPUTime() {
  66. ThreadMXBean bean = ManagementFactory.getThreadMXBean();
  67. return bean.isCurrentThreadCpuTimeSupported() ? bean.getCurrentThreadCpuTime() : 0L;
  68. }
  69. public String toMS() {
  70. String out = " WC(ms): " + elapsedWC / 1000000;
  71. out += "\n CPU(ms): " + elapsedCPU / 1000000;
  72. if (task.length() == 0)
  73. return out;
  74. else
  75. return task + "\n" + out;
  76. }
  77. public String noPreToMS() {
  78. // return "\n" + (elapsedWC / 1000000) + "\n" + (elapsedCPU / 1000000);
  79. return "" + (elapsedWC / 1000000) + "\n" + (elapsedCPU / 1000000);
  80. }
  81. public StopWatch divideByAndReturn(int n) {
  82. if (isOn) {
  83. try {
  84. throw new StopWatchException("StopWatch is still running");
  85. } catch (StopWatchException e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. StopWatch sw = new StopWatch(task);
  90. sw.elapsedWC = elapsedWC / n;
  91. sw.elapsedCPU = elapsedCPU / n;
  92. return sw;
  93. }
  94. public void divideBy(int n) {
  95. if (isOn) {
  96. try {
  97. throw new StopWatchException("StopWatch is still running");
  98. } catch (StopWatchException e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. elapsedWC /= n;
  103. elapsedCPU /= n;
  104. }
  105. public StopWatch addAndReturn(StopWatch s) {
  106. if (isOn || s.isOn) {
  107. try {
  108. throw new StopWatchException("StopWatch is still running");
  109. } catch (StopWatchException e) {
  110. e.printStackTrace();
  111. }
  112. }
  113. if (!task.equals(s.task)) {
  114. try {
  115. throw new StopWatchException("Tasks don't match: " + task + " != " + s.task);
  116. } catch (StopWatchException e) {
  117. e.printStackTrace();
  118. }
  119. }
  120. StopWatch sw = new StopWatch(task);
  121. sw.elapsedWC = elapsedWC + s.elapsedWC;
  122. sw.elapsedCPU = elapsedCPU + s.elapsedCPU;
  123. return sw;
  124. }
  125. public void add(StopWatch s) {
  126. if (isOn || s.isOn) {
  127. try {
  128. throw new StopWatchException("StopWatch is still running");
  129. } catch (StopWatchException e) {
  130. e.printStackTrace();
  131. }
  132. }
  133. // if (!task.equals(s.task)) {
  134. // try {
  135. // throw new StopWatchException("Tasks don't match: " + task + " != " + s.task);
  136. // } catch (StopWatchException e) {
  137. // e.printStackTrace();
  138. // }
  139. // }
  140. elapsedWC += s.elapsedWC;
  141. elapsedCPU += s.elapsedCPU;
  142. }
  143. }