timer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // base/timer.h
  2. // Copyright 2009-2011 Ondrej Glembek; Microsoft Corporation
  3. // See ../../COPYING for clarification regarding multiple authors
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  10. // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  11. // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  12. // MERCHANTABLITY OR NON-INFRINGEMENT.
  13. // See the Apache 2 License for the specific language governing permissions and
  14. // limitations under the License.
  15. #ifndef KALDI_BASE_TIMER_H_
  16. #define KALDI_BASE_TIMER_H_
  17. #include "base/kaldi-utils.h"
  18. #include "base/kaldi-error.h"
  19. #if defined(_MSC_VER) || defined(MINGW)
  20. namespace kaldi {
  21. class Timer {
  22. public:
  23. Timer() { Reset(); }
  24. // You can initialize with bool to control whether or not you want the time to
  25. // be set when the object is created.
  26. explicit Timer(bool set_timer) { if (set_timer) Reset(); }
  27. void Reset() {
  28. QueryPerformanceCounter(&time_start_);
  29. }
  30. double Elapsed() const {
  31. LARGE_INTEGER time_end;
  32. LARGE_INTEGER freq;
  33. QueryPerformanceCounter(&time_end);
  34. if (QueryPerformanceFrequency(&freq) == 0) {
  35. // Hardware does not support this.
  36. return 0.0;
  37. }
  38. return (static_cast<double>(time_end.QuadPart) -
  39. static_cast<double>(time_start_.QuadPart)) /
  40. (static_cast<double>(freq.QuadPart));
  41. }
  42. private:
  43. LARGE_INTEGER time_start_;
  44. };
  45. #else
  46. #include <sys/time.h>
  47. #include <unistd.h>
  48. namespace kaldi {
  49. class Timer {
  50. public:
  51. Timer() { Reset(); }
  52. // You can initialize with bool to control whether or not you want the time to
  53. // be set when the object is created.
  54. explicit Timer(bool set_timer) { if (set_timer) Reset(); }
  55. void Reset() { gettimeofday(&this->time_start_, &time_zone_); }
  56. /// Returns time in seconds.
  57. double Elapsed() const {
  58. struct timeval time_end;
  59. struct timezone time_zone;
  60. gettimeofday(&time_end, &time_zone);
  61. double t1, t2;
  62. t1 = static_cast<double>(time_start_.tv_sec) +
  63. static_cast<double>(time_start_.tv_usec)/(1000*1000);
  64. t2 = static_cast<double>(time_end.tv_sec) +
  65. static_cast<double>(time_end.tv_usec)/(1000*1000);
  66. return t2-t1;
  67. }
  68. private:
  69. struct timeval time_start_;
  70. struct timezone time_zone_;
  71. };
  72. #endif
  73. class Profiler {
  74. public:
  75. // Caution: the 'const char' should always be a string constant; for speed,
  76. // internally the profiling code uses the address of it as a lookup key.
  77. Profiler(const char *function_name): name_(function_name) { }
  78. ~Profiler();
  79. private:
  80. Timer tim_;
  81. const char *name_;
  82. };
  83. // To add timing info for a function, you just put
  84. // KALDI_PROFILE;
  85. // at the beginning of the function. Caution: this doesn't
  86. // include the class name.
  87. #define KALDI_PROFILE Profiler _profiler(__func__)
  88. } // namespace kaldi
  89. #endif // KALDI_BASE_TIMER_H_