phasicFlow/src/phasicFlow/Timer/Timer.hpp

169 lines
2.7 KiB
C++
Raw Normal View History

2022-09-01 17:05:35 +00:00
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
2022-12-09 22:02:54 +00:00
#ifndef __Timer_hpp__
#define __Timer_hpp__
2022-09-01 17:05:35 +00:00
#include <chrono>
2022-12-09 22:02:54 +00:00
#include "types.hpp"
2022-09-01 17:05:35 +00:00
namespace pFlow
{
// forward
class Timers;
class Timer
{
protected:
using timer = std::chrono::high_resolution_clock;
// - name for the timer
word name_ = "noNameTimer";
// start time
timer::time_point start_;
// number of times start() and end() are called
int32 numIteration_ = 0;
// sum of time duratios (in seconds) between all start() and end() calls
real accTime_ = 0.0;
//
real lastTime_ = 0.0;
Timers* parrent_ = nullptr;
public:
2022-12-09 22:02:54 +00:00
TypeInfo("Timer");
2022-09-01 17:05:35 +00:00
Timer(){}
Timer(const word name)
:
name_(name)
{}
Timer(const word name, Timers* parrent);
const word& name()const
{
return name_;
}
virtual ~Timer();
virtual void removeParrent()
{
parrent_ = nullptr;
}
virtual int32 level()const;
virtual bool master()const
{
return false;
}
void start()
{
start_ = timer::now();
}
void end()
{
auto end = timer::now();
lastTime_ = std::chrono::duration_cast
< std::chrono::duration<real> >(end - start_).count();
numIteration_++;
accTime_ += lastTime_;
}
2023-03-16 13:49:33 +00:00
inline
2022-09-01 17:05:35 +00:00
bool timerActive()const
{
return numIteration_!=0;
}
2023-03-16 13:49:33 +00:00
inline
2022-09-01 17:05:35 +00:00
real lastTime()const
{
return lastTime_;
}
2023-03-16 13:49:33 +00:00
inline
2022-09-01 17:05:35 +00:00
real totalTime()const
{
return accTime_;
}
2023-03-16 13:49:33 +00:00
inline
2022-09-01 17:05:35 +00:00
real averageTime()const
{
return accTime_/max(numIteration_, 1);
}
2023-03-16 13:49:33 +00:00
virtual
real accTimersTotal()const
{
return totalTime();
}
2022-09-01 17:05:35 +00:00
//// - IO operations
virtual bool write(iOstream& os, bool subTree)const;
virtual bool read(iIstream& is)
{
return true;
}
};
inline iOstream& operator<<(iOstream& os, const Timer& t)
{
t.write(os, false);
return os;
}
inline iIstream& operator>>(iIstream& is, Timer& t)
{
return is;
}
}
2022-12-09 22:02:54 +00:00
#endif //__Timer_hpp__