summaryrefslogtreecommitdiff
path: root/tnet_io/KaldiLib/Timer.h
blob: b220b931c007f0a99acc94609f8ece670d63b496 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef Timer_h
#define Timer_h

#include "Error.h"
#include <sstream>



#if defined(_WIN32) || defined(MINGW)

# include <windows.h>

namespace TNet
{
  class Timer {
  public:
    void 
    Start(void)
    {
      static int first = 1;

      if(first) {
              QueryPerformanceFrequency(&mFreq);
              first = 0;
      }
      QueryPerformanceCounter(&mTStart);
    }

    void 
    End(void)
    { QueryPerformanceCounter(&mTEnd); }

    double 
    Val()
    {
      return ((double)mTEnd.QuadPart - (double)mTStart.QuadPart) / 
        ((double)mFreq.QuadPart);
    }

  private:
    LARGE_INTEGER mTStart;
    LARGE_INTEGER mTEnd;
    LARGE_INTEGER mFreq;
  };
}

#else

# include <sys/time.h>
# include <unistd.h>

namespace TNet
{
  class Timer 
  {
  public:
    void 
    Start()
    { gettimeofday(&this->mTStart, &mTz); }

    void 
    End()
    { gettimeofday(&mTEnd,&mTz); }

    double 
    Val()
    {
      double t1, t2;

      t1 =  (double)mTStart.tv_sec + (double)mTStart.tv_usec/(1000*1000);
      t2 =  (double)mTEnd.tv_sec + (double)mTEnd.tv_usec/(1000*1000);
      return t2-t1;
    }

  private:
    struct timeval mTStart;
    struct timeval mTEnd;
    struct timezone mTz;
  };
}

#endif







///////////////////////////////////////////////////////////////
// Macros for adding the time intervals to time accumulator
#if PROFILING==1
#  define TIMER_START(timer) timer.Start()
#  define TIMER_END(timer,sum) timer.End(); sum += timer.Val()
#else
#  define TIMER_START(timer) 
#  define TIMER_END(timer,sum) 
#endif

#endif