Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset chrono::duration value?

Tags:

c++

c++-chrono

I want to collect the runtime of my program in pieces of codes(separate functions), current strategy is to calculate the execution time(chrono::duration) for each part and sum them together. But I have to deal with two different cases(call functions twice with different input), and I use a static variable timer to keep the separated durations. Now I wan to reset the variable before the second case. How can I do this? Of course I can use duration of two consecutive system_clock::now(), but this seems unnecessary. I tried timer = 0 or timer(0), doesn't work. Example:

class myClass {
  static std::chrono::milliseconds timer_A;
  void foo();
  void bar();
}

In cpp file:

std::chrono::milliseconds timer_A(0);
foo() {
  ...
  // get someDuration1;
  timer_A += someDuration1;
  ....
}
bar() {
  ...
  // get someDuration2;
  timer_A += someDuration2;
  ...
  cout << timer_A.count();
}

There is a main() will call foo() and bar() twice. i.e.,

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();
  }
like image 583
Emerson Xu Avatar asked Jun 07 '26 07:06

Emerson Xu


2 Answers

My answer is abit late but here goes:

timer_A = std::chrono::milliseconds::zero();

You can also create a duration template then use its Zero() function, that way you don't have to know what the duration is measured in (see below).

Chrono doesn't have its own timer class but you can create one very easily:

//----------------------------------------------------------------
/* 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<>;
like image 70
QuantumKarl Avatar answered Jun 10 '26 13:06

QuantumKarl


if it's millisecond type: timer_A = milliseconds(0);

like image 26
Emerson Xu Avatar answered Jun 10 '26 13:06

Emerson Xu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!