ViennaCL - The Vienna Computing Library  1.5.1
timer.hpp
Go to the documentation of this file.
1 #ifndef _VIENNACL_TOOLS_TIMER_HPP_
2 #define _VIENNACL_TOOLS_TIMER_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2014, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
21 
25 #include <iostream>
26 
27 
28 #ifdef _WIN32
29 
30 #define WINDOWS_LEAN_AND_MEAN
31 #include <windows.h>
32 #undef min
33 #undef max
34 
35 namespace viennacl{
36 
37  namespace tools{
38 
43  class timer
44  {
45  public:
46 
47  timer()
48  {
49  QueryPerformanceFrequency(&freq);
50  }
51 
52  void start()
53  {
54  QueryPerformanceCounter((LARGE_INTEGER*) &start_time);
55  }
56 
57  double get() const
58  {
59  LARGE_INTEGER end_time;
60  QueryPerformanceCounter((LARGE_INTEGER*) &end_time);
61  return (static_cast<double>(end_time.QuadPart) - static_cast<double>(start_time.QuadPart)) / static_cast<double>(freq.QuadPart);
62  }
63 
64 
65  private:
66  LARGE_INTEGER freq;
67  LARGE_INTEGER start_time;
68  };
69 
70  }
71 
72 }
73 
74 
75 #else
76 
77 #include <sys/time.h>
78 
79 namespace viennacl{
80 
81  namespace tools{
82 
87  class timer
88  {
89  public:
90 
91  timer() : ts(0)
92  {}
93 
94  void start()
95  {
96  struct timeval tval;
97  gettimeofday(&tval, NULL);
98  ts = static_cast<double>(tval.tv_sec * 1000000 + tval.tv_usec);
99  }
100 
101  double get() const
102  {
103  struct timeval tval;
104  gettimeofday(&tval, NULL);
105  double end_time = tval.tv_sec * 1000000 + tval.tv_usec;
106 
107  return static_cast<double>(end_time-ts) / 1000000.0;
108  }
109 
110  private:
111  double ts;
112  };
113 
114  }
115 
116 }
117 
118 
119 
120 #endif
121 
122 #endif
Simple timer class based on gettimeofday (POSIX) or QueryPerformanceCounter (Windows).
Definition: timer.hpp:87
timer()
Definition: timer.hpp:91
void start()
Definition: timer.hpp:94