All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 // Note: Sleep(float secs) is included in base/kaldi-utils.h.
24 
25 
26 #if defined(_MSC_VER) || defined(MINGW)
27 
28 namespace kaldi {
29 class Timer {
30  public:
31  Timer() { Reset(); }
32  void Reset() {
33  QueryPerformanceCounter(&time_start_);
34  }
35  double Elapsed() {
36  LARGE_INTEGER time_end;
37  LARGE_INTEGER freq;
38  QueryPerformanceCounter(&time_end);
39 
40  if (QueryPerformanceFrequency(&freq) == 0) {
41  // Hardware does not support this.
42  return 0.0;
43  }
44  return (static_cast<double>(time_end.QuadPart) -
45  static_cast<double>(time_start_.QuadPart)) /
46  (static_cast<double>(freq.QuadPart));
47  }
48  private:
49  LARGE_INTEGER time_start_;
50 };
51 }
52 
53 #else
54 #include <sys/time.h>
55 #include <unistd.h>
56 
57 namespace kaldi {
58 class Timer {
59  public:
60  Timer() { Reset(); }
61 
62  void Reset() { gettimeofday(&this->time_start_, &time_zone_); }
63 
65  double Elapsed() {
66  struct timeval time_end;
67  gettimeofday(&time_end, &time_zone_);
68  double t1, t2;
69  t1 = static_cast<double>(time_start_.tv_sec) +
70  static_cast<double>(time_start_.tv_usec)/(1000*1000);
71  t2 = static_cast<double>(time_end.tv_sec) +
72  static_cast<double>(time_end.tv_usec)/(1000*1000);
73  return t2-t1;
74  }
75 
76  private:
77  struct timeval time_start_;
78  struct timezone time_zone_;
79 };
80 }
81 
82 #endif
83 
84 
85 #endif // KALDI_BASE_TIMER_H_