pxar
 All Classes Namespaces Functions Variables Typedefs Friends
timer.h
1 #ifndef PXAR_TIMER_H
2 #define PXAR_TIMER_H
3 
4 #if ((defined WIN32) && (defined __CINT__))
5 #include <Windows4Root.h>
6 #else
7 #if (defined WIN32)
8 #include <Windows.h>
9 #else
10 #include <sys/time.h>
11 #include <ctime>
12 #endif //WIN32
13 #endif //WIN32 && CINT
14 
15 namespace pxar {
16 
17  class timer {
18  public:
21  timer() { start = GetTime(); }
22 
23  uint64_t get() { return static_cast<uint64_t>(GetTime() - start); }
24  private:
27  uint64_t start;
28 
32  uint64_t GetTime() {
33 #ifdef WIN32
34  // Windows
35  FILETIME ft;
36  LARGE_INTEGER li;
37 
38  // Get the amount of 100 nano seconds intervals elapsed since
39  // January 1, 1601 (UTC) and copy it to a LARGE_INTEGER structure.
40  GetSystemTimeAsFileTime(&ft);
41  li.LowPart = ft.dwLowDateTime;
42  li.HighPart = ft.dwHighDateTime;
43 
44  uint64_t ret = li.QuadPart;
45  // Convert from file time to UNIX epoch time.
46  ret -= 116444736000000000LL;
47  // From 100 nano seconds (10^-7) to 1 millisecond (10^-3) intervals
48  ret /= 10000;
49 
50  return ret;
51 #else
52  // Linux
53  struct timeval tv;
54 
55  gettimeofday(&tv, NULL);
56 
57  uint64_t ret = tv.tv_usec;
58  // Convert from micro seconds (10^-6) to milliseconds (10^-3)
59  ret /= 1000;
60 
61  // Adds the seconds (10^0) after converting them to milliseconds (10^-3)
62  ret += (tv.tv_sec * 1000);
63 
64  return ret;
65 #endif
66  }
67 
70  friend std::ostream & operator<<(std::ostream &out, timer &t) {
71  return out << static_cast<uint64_t>(t.GetTime() - t.start);
72  }
73 
76  friend std::ostream & operator<<(std::ostream &out, timer * t) {
77  return out << static_cast<uint64_t>(t->GetTime() - t->start);
78  }
79  };
80 
81 }
82 #endif
friend std::ostream & operator<<(std::ostream &out, timer &t)
Definition: timer.h:70
timer()
Definition: timer.h:21
friend std::ostream & operator<<(std::ostream &out, timer *t)
Definition: timer.h:76