ViennaCL - The Vienna Computing Library  1.7.1
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2016, 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 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 {
39 
44 class timer
45 {
46 public:
47 
48  timer()
49  {
50  QueryPerformanceFrequency(&freq);
51  }
52 
53  void start()
54  {
55  QueryPerformanceCounter((LARGE_INTEGER*) &start_time);
56  }
57 
58  double get() const
59  {
60  LARGE_INTEGER elapsed;
61  QueryPerformanceCounter((LARGE_INTEGER*) &end_time);
62  elapsed.QuadPart = end_time.QuadPart - start_time.QuadPart;
63  return elapsed.QuadPart / static_cast<double>(freq.QuadPart);
64  }
65 
66 
67 private:
68  LARGE_INTEGER freq;
69  LARGE_INTEGER start_time;
70  LARGE_INTEGER end_time;
71 };
72 
73 }
74 
75 }
76 
77 #else
78 
79 #include <sys/time.h>
80 
81 namespace viennacl
82 {
83 namespace tools
84 {
85 
90 class timer
91 {
92 public:
93 
94  timer() : ts(0)
95  {}
96 
97  void start()
98  {
99  struct timeval tval;
100  gettimeofday(&tval, NULL);
101  ts = static_cast<double>(tval.tv_sec * 1000000 + tval.tv_usec);
102  }
103 
104  double get() const
105  {
106  struct timeval tval;
107  gettimeofday(&tval, NULL);
108  double end_time = static_cast<double>(tval.tv_sec * 1000000 + tval.tv_usec);
109 
110  return static_cast<double>(end_time-ts) / 1000000.0;
111  }
112 
113 private:
114  double ts;
115 };
116 
117 }
118 }
119 
120 
121 
122 #endif
123 #endif
Simple timer class based on gettimeofday (POSIX) or QueryPerformanceCounter (Windows).
Definition: timer.hpp:90