StopWatch.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. }
  80. public StopWatch divideBy(int n) {
  81. if (isOn) {
  82. try {
  83. throw new StopWatchException("StopWatch is still running");
  84. } catch (StopWatchException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. StopWatch sw = new StopWatch(task);
  89. sw.elapsedWC = elapsedWC / n;
  90. sw.elapsedCPU = elapsedCPU / n;
  91. return sw;
  92. }
  93. public StopWatch addAndReturn(StopWatch s) {
  94. if (isOn || s.isOn) {
  95. try {
  96. throw new StopWatchException("StopWatch is still running");
  97. } catch (StopWatchException e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. if (!task.equals(s.task)) {
  102. try {
  103. throw new StopWatchException("Tasks don't match: " + task + " != " + s.task);
  104. } catch (StopWatchException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. StopWatch sw = new StopWatch(task);
  109. sw.elapsedWC = elapsedWC + s.elapsedWC;
  110. sw.elapsedCPU = elapsedCPU + s.elapsedCPU;
  111. return sw;
  112. }
  113. public void add(StopWatch s) {
  114. if (isOn || s.isOn) {
  115. try {
  116. throw new StopWatchException("StopWatch is still running");
  117. } catch (StopWatchException e) {
  118. e.printStackTrace();
  119. }
  120. }
  121. if (!task.equals(s.task)) {
  122. try {
  123. throw new StopWatchException("Tasks don't match: " + task + " != " + s.task);
  124. } catch (StopWatchException e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. elapsedWC += s.elapsedWC;
  129. elapsedCPU += s.elapsedCPU;
  130. }
  131. }