stop_watch.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __STOPWATCH_TIMER_H__
  17. #define __STOPWATCH_TIMER_H__
  18. #include <sys/time.h>
  19. class stopwatch_unit_t {
  20. public:
  21. class sec {
  22. public:
  23. static inline unsigned int gettime(void)
  24. {
  25. return time(NULL);
  26. };
  27. };
  28. class msec {
  29. public:
  30. static inline unsigned int gettime(void)
  31. {
  32. struct timeval tv;
  33. gettimeofday(&tv, NULL);
  34. return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  35. };
  36. };
  37. class usec {
  38. public:
  39. static inline unsigned int gettime(void)
  40. {
  41. struct timeval tv;
  42. gettimeofday(&tv, NULL);
  43. return tv.tv_sec * 1000000 + tv.tv_usec;
  44. };
  45. };
  46. };
  47. template <typename T> class stopwatch_t {
  48. private:
  49. unsigned int _value;
  50. public:
  51. inline stopwatch_t(void)
  52. {
  53. _value = 0;
  54. }
  55. inline stopwatch_t(bool started)
  56. {
  57. _value = started ? T::gettime() : 0;
  58. }
  59. inline stopwatch_t(unsigned int v)
  60. {
  61. _value = v;
  62. }
  63. inline stopwatch_t(const stopwatch_t &v)
  64. {
  65. _value = v._value;
  66. }
  67. inline ~stopwatch_t(void)
  68. {
  69. }
  70. /* start timer now */
  71. inline void start(void)
  72. {
  73. _value = T::gettime();
  74. }
  75. /* start from timestamp:v */
  76. inline void init(unsigned int v)
  77. {
  78. _value = v;
  79. }
  80. inline stopwatch_t &operator=(unsigned int v)
  81. {
  82. _value = v;
  83. return *this;
  84. }
  85. /* stop watch */
  86. inline void stop(void)
  87. {
  88. _value = T::gettime() - _value;
  89. }
  90. /* get counter _value */
  91. inline unsigned int get(void)
  92. {
  93. return _value;
  94. }
  95. inline operator unsigned int(void) const
  96. {
  97. return _value;
  98. }
  99. inline unsigned int live(void) const
  100. {
  101. return T::gettime() - _value;
  102. }
  103. };
  104. typedef stopwatch_t<stopwatch_unit_t::sec> stopwatch_sec_t;
  105. typedef stopwatch_t<stopwatch_unit_t::msec> stopwatch_msec_t;
  106. typedef stopwatch_t<stopwatch_unit_t::usec> stopwatch_usec_t;
  107. #endif