如何重置chrono::duration值
How to reset chrono::duration value?
我想收集我的程序运行时的代码片段(单独的函数),目前的策略是计算每个部分的执行时间(chrono::duration
)并将它们相加。但是我必须处理两种不同的情况(使用不同的输入两次调用函数),并且我使用静态变量timer
来保持分离的持续时间。现在我想在第二种情况之前重置变量。我该怎么做呢?当然,我可以使用两个连续的system_clock::now()的持续时间,但这似乎没有必要。我试过timer = 0
或timer(0)
,不起作用。例子:
class myClass {
static std::chrono::milliseconds timer_A;
void foo();
void bar();
}
在cpp文件:
std::chrono::milliseconds timer_A(0);
foo() {
...
// get someDuration1;
timer_A += someDuration1;
....
}
bar() {
...
// get someDuration2;
timer_A += someDuration2;
...
cout << timer_A.count();
}
有一个main()
将调用foo()
和bar()
两次。例如,
int main() {
if(someCondition) {
foo();
bar();
}
if(otherCondition) {
// here I need reset timer_A to 0 s.t. record the runtime again, how ??????????
foo();
bar();
}
我的回答是有点晚了,但是这里是:
timer_A = std::chrono::milliseconds::zero();
你也可以创建一个持续时间模板,然后使用它的Zero()函数,这样你就不需要知道持续时间是用什么度量的(见下文)。
Chrono没有自己的定时器类,但是你可以很容易地创建一个:
//----------------------------------------------------------------
/* Timer
Simple timer class using C++11 chrono.
example use:
{
Timer_t t; // use global typedef for standard timer.
t.Start(); // start the timer.
... do something you want to time ...
t.End(); // end the timer
t.CalcDuration; // Calculate and store (inside the Timer class) the interval between start and end.
cout << "Time in milliseconds: " << t.GetDuration().count() << end; // cout the interval
}
*/
template< typename precision = long double, typename ratio = std::milli >
class Timer final
{
public:
//---------------- aliases ----------------
using timeDuration_t = std::chrono::duration<precision, ratio>;
using timePoint_t = std::chrono::time_point<std::chrono::system_clock, timeDuration_t>;
using this_type = Timer< precision, ratio >;
//---------------- special member functions ----------------
// NOTE: all the default compiler generated member functions will suffice.
//---------------- member functions ----------------
//--------------------------------
/* Start()
Starts the timer.
*/
inline void Start()
{
m_start = std::chrono::system_clock::now();
}
//--------------------------------
/* End()
Ends the timer.
*/
inline void End()
{
m_end = std::chrono::system_clock::now();
}
//--------------------------------
/* CalcDuration()
Calculates the time elapsed (duration)
in between the previous calls to Start and End().
NOTE: make sure to have called Start() and End() before calling this function.
NOTE: Start() and End() can be called in any order.
*/
void CalcDuration()
{
m_duration = std::max( m_start, m_end ) - std::min( m_start, m_end );
}
//--------------------------------
/* GetDuration()
Returns the calculated duration.
NOTE: make sure to call CalcDuration() before calling this function.
*/
timeDuration_t const& GetDuration() const
{
return m_duration;
}
//--------------------------------
/* Zero()
Zeros the internal members, effectively resetting the Timer.
*/
void Zero()
{
m_start = timeDuration_t::zero();
m_end = m_start;
m_duration = m_end;
}
//--------------------------------
/* TestLatency( i_count )
Tests the latency / error of the timer class.
NOTE: this number is how inaccurate your timings can be.
*/
static timeDuration_t TestLatency( size_t const i_count = 512 )
{
this_type t;
timeDuration_t tSum = timeDuration_t::duration::zero();
for( size_t i = 0; i < i_count; ++i )
{
t.Start();
t.End();
t.CalcDuration();
tSum += t.GetDuration();
}
return tSum / i_count;
}
private:
//---------------- private member data ----------------
timePoint_t m_start;
timePoint_t m_end;
timeDuration_t m_duration;
};
using Timer_t = Timer<>;
如果是毫秒类型:timer_A = milliseconds(0);
相关文章:
- 从持续时间构造std::chrono::system_clock::time_point
- 如何在c++迭代器类型中包装std::chrono
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 使用 memcpy() 复制到 std::chrono::milliseconds 会给出错误 -Werror=clas
- std::multimap<std::chrono::milliseconds, T>::rbegin 在 MSVS-13 中指向 end()?
- 如何在C++中将 chrono::秒转换为 HH:MM:SS 格式的字符串?
- C++ Chrono 确定一天是否是周末?
- std::adjacent_difference with std::chrono time_point
- VSCode 说 std::chrono 是模棱两可的,如果运算符<<重载
- 是否确保 2 个连续的 std::chrono::steady_clock::now() 不相等?
- std::chrono::duration::count函数的实际结果类型是什么
- std::hash for std::chrono::duration
- 尝试转换 std::chrono::d uration 会导致"rep cannot be a duration"编译错误
- SFINAE将实例化类型限制为std::chrono::duration类型
- Example of C++ 11 std::chrono::duration
- std线程sleep_for不适用于某些chrono::duration
- std::chrono::duration::duration()怎么可能是constexpr
- 将秒转换为std::chrono::duration
- 带有std::chrono::duration的用户定义重载操作符*
- 如何重置chrono::duration值