c++ - How to reset chrono::duration value? -
i want collect runtime of program in pieces of codes(separate functions), current strategy calculate execution time(chrono::duration
) each part , sum them together. have deal 2 different cases(call functions twice different input), , use static variable timer
keep separated durations. wan reset variable before second case. how can this? of course can use duration of 2 consecutive system_clock::now(), seems unnecessary. 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() { ... // someduration1; timer_a += someduration1; .... } bar() { ... // someduration2; timer_a += someduration2; ... cout << timer_a.count(); }
there main()
call foo()
, bar()
twice. i.e.,
int main() { if(somecondition) { foo(); bar(); } if(othercondition) { // here need reset timer_a 0 s.t. record runtime again, how ?????????? foo(); bar(); }
my answer abit late here goes:
timer_a = std::chrono::miliseconds::zero();
you can create duration template use zero() function, way don't have know duration measured in (see below).
chrono doesn't have own timer class can create 1 easily:
//---------------------------------------------------------------- /* timer simple timer class using c++11 chrono. example use: { timer_t t; // use global typedef standard timer. t.start(); // start timer. ... want time ... t.end(); // end timer t.calcduration; // calculate , store (inside timer class) interval between start , end. cout << "time in milliseconds: " << t.getduration().count() << end; // cout 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: default compiler generated member functions suffice. //---------------- member functions ---------------- //-------------------------------- /* start() starts timer. */ inline void start() { m_start = std::chrono::system_clock::now(); } //-------------------------------- /* end() ends timer. */ inline void end() { m_end = std::chrono::system_clock::now(); } //-------------------------------- /* calcduration() calculates time elapsed (duration) in between previous calls start , end(). note: make sure have called start() , end() before calling function. note: start() , end() can called in order. */ void calcduration() { m_duration = std::max( m_start, m_end ) - std::min( m_start, m_end ); } //-------------------------------- /* getduration() returns calculated duration. note: make sure call calcduration() before calling function. */ timeduration_t const& getduration() const { return m_duration; } //-------------------------------- /* zero() zeros internal members, resetting timer. */ void zero() { m_start = timeduration_t::zero(); m_end = m_start; m_duration = m_end; } //-------------------------------- /* testlatency( i_count ) tests latency / error of timer class. note: number how inaccurate 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 = 0; < 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<>;
Comments
Post a Comment