首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重置chrono::duration值?

如何重置chrono::duration值?
EN

Stack Overflow用户
提问于 2015-07-24 05:39:56
回答 2查看 4.4K关注 0票数 1

我想收集我的程序的运行时在代码片段(单独的函数),当前的策略是计算每个部分的执行时间(chrono::duration),并将它们相加。但我必须处理两种不同的情况(使用不同的输入调用函数两次),并且我使用静态变量timer来保存分隔的持续时间。现在我想在第二个例子之前重置变量。我该怎么做呢?当然,我可以使用两个连续的system_clock::now()的持续时间,但这似乎没有必要。我试过timer = 0timer(0),不起作用。示例:

代码语言:javascript
复制
class myClass {
  static std::chrono::milliseconds timer_A;
  void foo();
  void bar();
}

在cpp文件中:

代码语言:javascript
复制
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()两次。即,

代码语言:javascript
复制
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();
  }
EN

回答 2

Stack Overflow用户

发布于 2016-10-20 00:24:15

我的回答有点晚,但这里是这样的:

代码语言:javascript
复制
timer_A = std::chrono::milliseconds::zero();

您还可以创建一个持续时间模板,然后使用它的Zero()函数,这样您就不必知道持续时间是用什么来度量的(见下文)。

Chrono没有自己的timer类,但你可以很容易地创建一个:

代码语言:javascript
复制
//----------------------------------------------------------------
/* 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<>;
票数 5
EN

Stack Overflow用户

发布于 2015-07-24 07:16:02

如果是毫秒类型:timer_A = milliseconds(0);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31598516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档