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