Flow123d  release_2.1.0-87-gfbc1563
time_point.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file time_point.hh
15  * @brief
16  */
17 
18 #ifndef TIMEPOINT_HH_
19 #define TIMEPOINT_HH_
20 
21 #include "config.h"
22 #include <string>
23 #include <time.h>
24 
25 /**
26  * Include either Windows or chrono lib
27  */
28 #ifdef FLOW123D_HAVE_TIMER_QUERY_PERFORMANCE_COUNTER
29 #include <windows.h>
30 #else
31 #include <chrono>
32 #endif //FLOW123D_HAVE_TIMER_CHRONO_HIGH_RESOLUTION
33 
34 using namespace std;
35 
36 /**
37  * Class TimerData serves for getting current time in ticks units.
38  * TimerData overloads standard + and - operators so object can be
39  * subtracted/added from/to one another.
40  *
41  * TimerData uses one if the following timers based
42  * on set definition namely:
43  *
44  * 1) FLOW123D_HAVE_TIMER_QUERY_PERFORMANCE_COUNTER
45  * It is used Timer for Windows platform only.
46  *
47  * Method QueryPerformanceCounter populates given
48  * LARGE_INTEGER object with timer informations.
49  * Resolution if this timer is several hundreds ns.
50  * Tests have shown average about 300 ns.
51  * Maximum resolution is 1 ns.
52  *
53  *
54  * 2) FLOW123D_HAVE_TIMER_CHRONO_HIGH_RESOLUTION (default without definition set)
55  * It is used standard multiplatform Chrono Timer
56  * which is available since C++11 (compile flag -std=cxx11 required!)
57  *
58  * Resolution of timer depends on HW but tests shown results fluctuating around 1000 ns.
59  * Maximum resolution is 1 ns.
60  *
61  *
62  * Note:
63  * Standard clock() method available in c++ yields
64  * resolution in units of milliseconds (roughly 10 ms)
65  * but some compilers replaces during compilation
66  * method clock() with better Timer having resolution 1000 ns.
67  * Meaning clock() method is not sufficient or stable
68  * for time measurements.
69  * Maximum resolution is 1000 ns (limited by value of CLOCKS_PER_SEC).
70  *
71  * gettimeofday
72  * The actual resolution of gettimeofday() depends on the hardware architecture. Intel
73  * processors as well as SPARC machines offer high resolution timers that measure microseconds.
74  * Other hardware architectures fall back to the system’s timer, which is typically set to 100 Hz.
75  * In such cases, the time resolution will be less accurate. gettimeofday() can result in incorrect
76  * timings if there are processes on your system that change the timer
77  * Possible solution may be other clock
78  *
79  * clock_gettime (CLOCK_MONOTONIC) with clock_getres() method
80  * but clock_gettime is present only on newest Linux. other system have only gettimeofday()
81  */
82 class TimePoint {
83  public:
84 
85  /**
86  * Constructor will populate object with current time
87  */
88  TimePoint ();
89 
90  /**
91  * Overloaded operator for subtraction. Allows subtracting TimerPoint objects using '-' sign
92  * Used for determining interval between two TimerPoints
93  *
94  * Returns duration in seconds
95  */
96  double operator- (const TimePoint &right);
97 
98  /**
99  * Internal variable for storing actual ticks
100  */
101  long long ticks;
102 
103  private:
104 
105 #ifdef FLOW123D_HAVE_TIMER_QUERY_PERFORMANCE_COUNTER
106  /**
107  * Variable for storing CPU Frequency
108  */
109  static LARGE_INTEGER frequency;
110 
111  /**
112  * Init function which sets current CPU frequency
113  * and returns it
114  */
115  static LARGE_INTEGER get_frequency ();
116 #endif //FLOW123D_HAVE_TIMER_QUERY_PERFORMANCE_COUNTER
117 
118 };
119 #endif /* TIMEPOINT_HH_ */
long long ticks
Definition: time_point.hh:101