TimeMeasure是我以前写的一个java 辅助类,开发人员可以用它们在控制台打印任意两个步骤之间的时间消耗。中途可以pause这个时间计数方法,然后再resume回来,pause的时间是不计入到实际方法消耗的时间的。使用方法见TimeMeasure的main方法,另外如果你愿意,可以拓展它用log4j或者其他方式输出到文件。

 

TimeMeasure.java

 

Java代码 一个java时间辅助工具类 _辅助 一个java时间辅助工具类 _java_02
  1. public class TimeMeasure {
  2.  
  3. // PTODO create junit test class
  4. private static HashMap<String, TimeStack> timers;
  5.  
  6. private static int indent = 0;
  7.  
  8. /**
  9. * measureActive is true by default. A true value means that all methods calls are processed else no one.
  10. */
  11. public static boolean measureActive = true;
  12.  
  13. /**
  14. * display is true by default. A true value means that all informations are displayed.
  15. */
  16. public static boolean display = true;
  17.  
  18. public static boolean displaySteps = true;
  19.  
  20.  
  21.  
  22. /**
  23. *
  24. * DOC hywang Comment method "start".
  25. *
  26. * @param idTimer
  27. */
  28. public static void begin(String idTimer) {
  29. if (!measureActive) {
  30. return;
  31. }
  32. init();
  33. if (timers.containsKey(idTimer)) {
  34. if (display) {
  35. System.out.println(indent(indent) + "Warning (start): timer " + idTimer + " already exists"); //$NON-NLS-1$ //$NON-NLS-2$
  36. }
  37. } else {
  38. indent++;
  39. TimeStack times = new TimeStack();
  40. timers.put(idTimer, times);
  41. if (display) {
  42. System.out.println(indent(indent) + "Start '" + idTimer + "' ..."); //$NON-NLS-1$ //$NON-NLS-2$
  43. }
  44. }
  45. }
  46.  
  47. /**
  48. *
  49. * DOC hywang Comment method "end".
  50. *
  51. * @param idTimer
  52. * @return total elapsed time since start in ms
  53. */
  54. public static long end(String idTimer) {
  55. if (!measureActive) {
  56. return 0;
  57. }
  58. init();
  59. if (!timers.containsKey(idTimer)) {
  60. if (display) {
  61. System.out.println(indent(indent) + "Warning (end): timer " + idTimer + " doesn't exist"); //$NON-NLS-1$ //$NON-NLS-2$
  62. }
  63. return -1;
  64. } else {
  65. TimeStack timeStack = timers.get(idTimer);
  66. timers.remove(idTimer);
  67. long elapsedTimeSinceLastRequest = timeStack.getLastStepElapsedTime();
  68. if (display && displaySteps) {
  69. System.out.println(indent(indent) + "End '" + idTimer + "', elapsed time since last request: " //$NON-NLS-1$ //$NON-NLS-2$
  70. + elapsedTimeSinceLastRequest + " ms "); //$NON-NLS-1$
  71. }
  72. long totalElapsedTime = timeStack.getTotalElapsedTime();
  73. if (display) {
  74. System.out.println(indent(indent) + "End '" + idTimer + "', total elapsed time: " + totalElapsedTime + " ms "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  75. }
  76. indent--;
  77. return totalElapsedTime;
  78. }
  79. }
  80.  
  81. /**
  82. *
  83. * DOC hywang Comment method "timeSinceBegin".
  84. *
  85. * @param idTimer
  86. * @return total elapsed time since start in ms
  87. */
  88. public static long timeSinceBegin(String idTimer) {
  89. if (!measureActive) {
  90. return 0;
  91. }
  92. init();
  93. if (!timers.containsKey(idTimer)) {
  94. if (display) {
  95. System.out.println(indent(indent) + "Warning (end): timer " + idTimer + " does'nt exist"); //$NON-NLS-1$ //$NON-NLS-2$
  96. }
  97. return -1;
  98. } else {
  99. long time = timers.get(idTimer).getTotalElapsedTime();
  100. if (display) {
  101. System.out.println(indent(indent) + "-> '" + idTimer + "', elapsed time since start: " + time + " ms "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  102. }
  103. return time;
  104. }
  105. }