Flow123d  JS_before_hm-979-g397e552
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 
22 using namespace std;
23 
24 /**
25  * Class TimerData serves for getting current time in ticks units.
26  * TimerData overloads standard + and - operators so object can be
27  * subtracted/added from/to one another.
28  *
29  * TimerData uses one if the following timers based
30  * on set definition namely:
31  *
32  * 1) FLOW123D_HAVE_TIMER_QUERY_PERFORMANCE_COUNTER
33  * It is used Timer for Windows platform only.
34  *
35  * Method QueryPerformanceCounter populates given
36  * LARGE_INTEGER object with timer informations.
37  * Resolution if this timer is several hundreds ns.
38  * Tests have shown average about 300 ns.
39  * Maximum resolution is 1 ns.
40  *
41  *
42  * 2) FLOW123D_HAVE_TIMER_CHRONO_HIGH_RESOLUTION (default without definition set)
43  * It is used standard multiplatform Chrono Timer
44  * which is available since C++11 (compile flag -std=cxx11 required!)
45  *
46  * Resolution of timer depends on HW but tests shown results fluctuating around 1000 ns.
47  * Maximum resolution is 1 ns.
48  *
49  *
50  * Note:
51  * Standard clock() method available in c++ yields
52  * resolution in units of milliseconds (roughly 10 ms)
53  * but some compilers replaces during compilation
54  * method clock() with better Timer having resolution 1000 ns.
55  * Meaning clock() method is not sufficient or stable
56  * for time measurements.
57  * Maximum resolution is 1000 ns (limited by value of CLOCKS_PER_SEC).
58  *
59  * gettimeofday
60  * The actual resolution of gettimeofday() depends on the hardware architecture. Intel
61  * processors as well as SPARC machines offer high resolution timers that measure microseconds.
62  * Other hardware architectures fall back to the system’s timer, which is typically set to 100 Hz.
63  * In such cases, the time resolution will be less accurate. gettimeofday() can result in incorrect
64  * timings if there are processes on your system that change the timer
65  * Possible solution may be other clock
66  *
67  * clock_gettime (CLOCK_MONOTONIC) with clock_getres() method
68  * but clock_gettime is present only on newest Linux. other system have only gettimeofday()
69  */
70 class TimePoint {
71  public:
72 
73  /**
74  * Constructor will populate object with current time
75  */
76  TimePoint ();
77 
78  /**
79  * Overloaded operator for subtraction. Allows subtracting TimerPoint objects using '-' sign
80  * Used for determining interval between two TimerPoints
81  *
82  * Returns duration in seconds
83  */
84  double operator- (const TimePoint &right);
85 
86  /**
87  * Internal variable for storing actual ticks
88  */
89  long long ticks;
90 
91  private:
92 
93 #ifdef FLOW123D_HAVE_TIMER_QUERY_PERFORMANCE_COUNTER
94  /**
95  * Variable for storing CPU Frequency
96  */
97  static LARGE_INTEGER frequency;
98 
99  /**
100  * Init function which sets current CPU frequency
101  * and returns it
102  */
103  static LARGE_INTEGER get_frequency ();
104 #endif //FLOW123D_HAVE_TIMER_QUERY_PERFORMANCE_COUNTER
105 
106 };
107 #endif /* TIMEPOINT_HH_ */
long long ticks
Definition: time_point.hh:89