Timer.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #ifndef __Timerr_hpp__
23 #define __Timerr_hpp__
24 
25 
26 #include <chrono>
27 
28 #include "types.hpp"
29 
30 
31 
32 namespace pFlow
33 {
34 
35 // forward
36 class Timers;
37 
38 
39 class Timer
40 {
41 protected:
42 
43  using timer = std::chrono::high_resolution_clock;
44 
45  // - name for the timer
46  word name_ = "noNameTimer";
47 
48  // start time
49  timer::time_point start_;
50 
51  // number of times start() and end() are called
53 
54  // sum of time duratios (in seconds) between all start() and end() calls
55  real accTime_ = 0.0;
56 
57  //
58  real lastTime_ = 0.0;
59 
60  Timers* parrent_ = nullptr;
61 
62 public:
63 
64  TypeInfo("Timer");
65 
66  Timer(){}
67 
68  Timer(const word name)
69  :
70  name_(name)
71  {}
72 
73 
74  Timer(const word name, Timers* parrent);
75 
76 
77  const word& name()const
78  {
79  return name_;
80  }
81 
82  virtual ~Timer();
83 
84  virtual void removeParrent()
85  {
86  parrent_ = nullptr;
87  }
88 
89  virtual int32 level()const;
90 
91 
92  virtual bool master()const
93  {
94  return false;
95  }
96 
97  void start()
98  {
99  start_ = timer::now();
100  }
101 
102  void end()
103  {
104  auto end = timer::now();
105  lastTime_ = std::chrono::duration_cast
106  < std::chrono::duration<real> >(end - start_).count();
107 
108  numIteration_++;
109  accTime_ += lastTime_;
110  }
111 
112  inline
113  bool timerActive()const
114  {
115  return numIteration_!=0;
116  }
117 
118  inline
119  real lastTime()const
120  {
121  return lastTime_;
122  }
123 
124  inline
126  {
127  return accTime_;
128  }
129 
130  inline
132  {
133 
134  return accTime_/max(numIteration_, 1);
135  }
136 
137  virtual
139  {
140  return totalTime();
141  }
142 
144 
145  virtual bool write(iOstream& os, bool subTree)const;
146 
147  virtual bool read(iIstream& is)
148  {
149  return true;
150  }
151 };
152 
153 
154 inline iOstream& operator<<(iOstream& os, const Timer& t)
155 {
156  t.write(os, false);
157  return os;
158 }
159 
161 {
162  return is;
163 }
164 
165 }
166 
167 
168 #endif //__Timer_hpp__
pFlow::Timer::averageTime
real averageTime() const
Definition: Timer.hpp:131
pFlow::real
float real
Definition: builtinTypes.hpp:46
pFlow::Timer::start
void start()
Definition: Timer.hpp:97
pFlow::Timer::name_
word name_
Definition: Timer.hpp:46
types.hpp
pFlow::Timer::totalTime
real totalTime() const
Definition: Timer.hpp:125
pFlow::Timer::removeParrent
virtual void removeParrent()
Definition: Timer.hpp:84
pFlow::Timer::numIteration_
int32 numIteration_
Definition: Timer.hpp:52
pFlow::Timer::write
virtual bool write(iOstream &os, bool subTree) const
Definition: Timer.cpp:52
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::Timers
Definition: Timers.hpp:33
pFlow::Timer::start_
timer::time_point start_
Definition: Timer.hpp:49
pFlow::Timer::Timer
Timer()
Definition: Timer.hpp:66
pFlow
Definition: demComponent.hpp:28
pFlow::Timer::lastTime
real lastTime() const
Definition: Timer.hpp:119
pFlow::Timer::parrent_
Timers * parrent_
Definition: Timer.hpp:60
pFlow::Timer::end
void end()
Definition: Timer.hpp:102
pFlow::Timer::~Timer
virtual ~Timer()
Definition: Timer.cpp:35
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::Timer::lastTime_
real lastTime_
Definition: Timer.hpp:58
pFlow::Timer
Definition: Timer.hpp:39
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::Timer::Timer
Timer(const word name)
Definition: Timer.hpp:68
pFlow::count
auto count(const Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:26
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::Timer::level
virtual int32 level() const
Definition: Timer.cpp:43
pFlow::max
T max(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:164
pFlow::Timer::accTime_
real accTime_
Definition: Timer.hpp:55
pFlow::Timer::name
const word & name() const
Definition: Timer.hpp:77
pFlow::Timer::master
virtual bool master() const
Definition: Timer.hpp:92
pFlow::Timer::accTimersTotal
virtual real accTimersTotal() const
Definition: Timer.hpp:138
pFlow::Timer::read
virtual bool read(iIstream &is)
Definition: Timer.hpp:147
pFlow::Timer::timer
std::chrono::high_resolution_clock timer
Definition: Timer.hpp:43
pFlow::Timer::TypeInfo
TypeInfo("Timer")
pFlow::Timer::timerActive
bool timerActive() const
Definition: Timer.hpp:113
pFlow::iOstream
Definition: iOstream.hpp:53