Geom Software - C++ Programming and Geometry Libraries
WOF Documentation pages v1.19
TimerC.h
1 // Copyright (C) Geom Software e.U, Bernhard Kornberger, Graz/Austria
2 //
3 // This file is part of the WOF software. WOF is commercial software.
4 // Users holding a license may use this file in accordance with the
5 // License Agreement.
6 //
7 // This software is provided AS IS with NO WARRANTY OF ANY KIND,
8 // INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
9 // FOR A PARTICULAR PURPOSE.
10 //
11 // Please contact the author if any conditions of this licensing are
12 // not clear to you.
13 //
14 // Author: Bernhard Kornberger, bkorn (at) geom.at
15 // http://www.geom.at
16 
17 #pragma once
18 #include <chrono>
19 #include <string>
20 #include <iostream>
21 namespace GEOM_WOF {
22 
27 class TimerC
28 {
29 public:
34  TimerC():
35  t0(std::chrono::high_resolution_clock::now()),
36  elapsedSeconds(0.0),
37  bStopped(false)
38  {}
43  double stop()
44  {
45  bStopped=true;
46  elapsedSeconds = std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - t0).count();
47  return elapsedSeconds;
48  }
55  double get() const
56  {
57  if(bStopped)
58  {
59  return elapsedSeconds;
60  }
61  else
62  {
63  return std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - t0).count();
64  }
65  }
71  {
72  double elapsed(std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - t0).count());
73  t0=std::chrono::high_resolution_clock::now();
74  return elapsed;
75  }
76 
82  void report(const std::string& s)
83  {
84  double elapsed(std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - t0).count());
85  t0=std::chrono::high_resolution_clock::now();
86  std::cout<<"\nReport ["<<s<<"]: "<<elapsed<<" s"<<std::endl;
87  }
88 private:
89  std::chrono::high_resolution_clock::time_point t0;
90  double elapsedSeconds;
91  bool bStopped;
92 };
93 
94 
95 
96 
98 class Dur {
99 public:
100  Dur():numIntervals(0),totalTime(0.0)
101  {
102  }
103 
104  void reset()
105  {
106  numIntervals=0;
107  totalTime=0.0;
108  ss.clear();
109  ss.str("");
110  }
111  void start()
112  {
113  ++numIntervals;
114  startTime = std::chrono::high_resolution_clock::now();
115  }
116 
117  double stop()
118  {
119  auto endTime = std::chrono::high_resolution_clock::now();
120  double elapsed_ns = std::chrono::duration<double, std::nano>(endTime - startTime).count();
121  totalTime += elapsed_ns;
122  return elapsed_ns;
123  }
124  // Reports the time since the last start() or report() call, the measurement is continued
125  // bSilent caches the output. It can later be printed with 'unsilence()'
126  double report(const std::string& name,bool bSilent=false)
127  {
128  auto cur = std::chrono::high_resolution_clock::now();
129  double elapsed_ns = std::chrono::duration<double, std::nano>(cur - startTime).count();
130  totalTime += elapsed_ns;
131  if(bSilent) ss<<"Dur ["<<name<<"]: "<<elapsed_ns<<" ns ("<<elapsed_ns/1e9<<" s, sum="<<totalTime<<" ns ( " << (totalTime/1e9) << ")s. NumIntervals="<<numIntervals<<"\n";
132  else std::cout<<"Dur ["<<name<<"]: "<<elapsed_ns<<" ns ("<<elapsed_ns/1e9<<" s), sum="<<totalTime<<" ns ( " << (totalTime/1e9) << ")s. NumIntervals="<<numIntervals<<std::endl;
133  start(); // Counts up numIntervals
134  return elapsed_ns;
135  }
136  // Stops the time measurement, reports the time since the last start() or report() call
137  double stopAndReport(const std::string& name)
138  {
139  auto cur = std::chrono::high_resolution_clock::now();
140  double elapsed_ns = std::chrono::duration<double, std::nano>(cur - startTime).count();
141  totalTime += elapsed_ns;
142  std::cout<<"Dur ["<<name<<"]: "<<elapsed_ns<<" ns ("<<elapsed_ns/1e9<<" s), sum="<<totalTime<<" ns ( " << (totalTime/1e9) << " s). NumIntervals="<<numIntervals<<". Stop.\n"<<std::endl;
143  return elapsed_ns;
144  }
145  // Reports the whole elapsed time (of an already stopped Dur)
146  double reportTotal(const std::string& name)
147  {
148  std::cout<<"Dur ["<<name<<"]: total time: "<<totalTime<<" ns ( " << (totalTime/1e9) << " s). NumIntervals="<<numIntervals<<". Stop.\n"<<std::endl;
149  return totalTime;
150  }
151  void unsilence()
152  {
153  std::cout<<"Dur::unsilence():\n";
154  std::cout<<ss.str()<<std::endl;
155  ss.clear();
156  ss.str("");
157  }
158  double getSum() const
159  {
160  return totalTime;
161  }
162 
163 private:
164  int numIntervals;
165  double totalTime;
166  std::chrono::high_resolution_clock::time_point startTime;
167  std::stringstream ss;
168 };
169 
170 
171 } // Namespace
Timer class.
Definition: TimerC.h:28
double stop()
Timer stop.
Definition: TimerC.h:43
double get() const
Get the elapsed time.
Definition: TimerC.h:55
void report(const std::string &s)
Report.
Definition: TimerC.h:82
TimerC()
Constructor.
Definition: TimerC.h:34
double getSecondsSinceLastReport()
Get elapsed time since last report.
Definition: TimerC.h:70
Definition: Point3.h:23