ug4
stopwatch.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp, Torbjoern Klatt
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
43 #ifndef __H__UG__STOPWATCH_H__
44 #define __H__UG__STOPWATCH_H__
45 
46 // The following define is a leftover from pre-C++11 times.
47 // Def or undef to switch between the two implementations.
48 #define UG_CXX11
49 
50 #include <iostream>
51 
52 #ifdef UG_CXX11
53 #include <chrono>
54 #else
55 #include <ctime>
56 #endif
57 
58 #ifdef UG_POSIX
59  #include <sys/time.h>
60 #endif
61 
62 namespace ug
63 {
64 
65 
66 
68 
69  public:
71 
72  void tic(){m_tlaunch = clock();}
73 
75  double toc()
76  {
77  m_tdone = clock();
78  //std::cerr << m_tdone;
79  clock_t delta= (m_tdone-m_tlaunch);
80  m_ttotal += delta;
81  return 1.0*delta/CLOCKS_PER_SEC;
82  }
83 
85  double cuckoo() {return 1.0*m_ttotal/ CLOCKS_PER_SEC;}
86 
87 protected:
88 
89  clock_t m_tlaunch;
90  clock_t m_tdone;
91  clock_t m_ttotal;
92 };
93 
94 
95 #ifdef UG_POSIX
96  inline double get_clock_s()
97  {
98  timeval time;
99  gettimeofday(&time, NULL);
100  return time.tv_sec + time.tv_usec/1000000.0;
101  }
102 #else
103  inline double get_clock_s()
104  {
105  return clock() / ((double)CLOCKS_PER_SEC);
106  }
107 #endif
108 
111 
124 {
125  public:
130  // you cant be really sure when constructor is called
131 #ifdef UG_CXX11
132  begin = std::chrono::high_resolution_clock::now();
133  end = std::chrono::high_resolution_clock::now();
134 #else
135  beg = end = get_clock_s();
136 #endif
137  bRunning = false;
138  }
139 
143  void start() {
144  std::cout.flush();
145 #ifdef UG_CXX11
146  begin = std::chrono::high_resolution_clock::now();
147 #else
148  beg = get_clock_s();
149 #endif
150  bRunning = true;
151  }
152 
156  void stop() {
157 #ifdef UG_CXX11
158  end = std::chrono::high_resolution_clock::now();
159 #else
160  end = get_clock_s();
161 #endif
162  bRunning = false;
163  }
164 
175  friend std::ostream &operator << ( std::ostream &out, Stopwatch &s ) {
176  out << s.ms() << " ms";
177  return out;
178  }
179 
191  double ms() {
192 #ifdef UG_CXX11
193  if ( bRunning ) end = std::chrono::high_resolution_clock::now();
194  return std::chrono::duration_cast<std::chrono::milliseconds> (end-begin).count();
195 #else
196  if( bRunning ) end = get_clock_s();
197  return ( end - beg ) * 1000.0;
198 #endif
199  }
200 
201  private:
202 #ifdef UG_CXX11
204  std::chrono::high_resolution_clock::time_point begin;
206 
207  std::chrono::high_resolution_clock::time_point end;
208 #else
210  double beg;
212  double end;
213 #endif
215  bool bRunning;
216 };
217 
218 // end group ugbase_common
220 
221 } // namespace ug
222 
223 
224 #endif // __H__UG__STOPWATCH_H__
parameterString s
Definition: stopwatch.h:67
double cuckoo()
returns total time (in seconds)
Definition: stopwatch.h:85
clock_t m_tlaunch
Definition: stopwatch.h:89
double toc()
returns time since last tic
Definition: stopwatch.h:75
void tic()
Definition: stopwatch.h:72
clock_t m_tdone
Definition: stopwatch.h:90
clock_t m_ttotal
Definition: stopwatch.h:91
CuckooClock()
Definition: stopwatch.h:70
Stopwatch class for quickly taking times.
Definition: stopwatch.h:124
void stop()
Stops the Stopwatch.
Definition: stopwatch.h:156
void start()
Starts the Stopwatch.
Definition: stopwatch.h:143
double ms()
Returns milliseconds since call of start.
Definition: stopwatch.h:191
std::chrono::high_resolution_clock::time_point end
Number of microseconds since begin.
Definition: stopwatch.h:207
std::chrono::high_resolution_clock::time_point begin
Time point of the start of Stopwatch.
Definition: stopwatch.h:204
bool bRunning
Flag indicating state of Stopwatch.
Definition: stopwatch.h:215
friend std::ostream & operator<<(std::ostream &out, Stopwatch &s)
Prints number of milliseconds since call of start() to ostream.
Definition: stopwatch.h:175
Stopwatch()
Default constructor for the Stopwatch.
Definition: stopwatch.h:129
the ug namespace
double get_clock_s()
Definition: stopwatch.h:103