Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get duration, as int milli's and float seconds from <chrono>?

I'm trying to use chrono library for timers and durations.

I want to be able to have a Duration frameStart; ( from app start ) and a Duration frameDelta; ( time between frames )

I need to be able to get the frameDelta duration as milliseconds and float seconds.

How do you do this with the new c++11 <chrono> libraries? I've been working on it and googling ( information is sparse ). The code is heavily templated and requires special casts and things, I can't figure out how to use this library correctly.

like image 700
EddieV223 Avatar asked Jan 18 '13 01:01

EddieV223


People also ask

Which Chrono function allows us to convert a clock duration from one unit to the other?

std::chrono::duration_cast Converts the value of dtn into some other duration type, taking into account differences in their periods. The function does not use implicit conversions.

What is std :: Chrono :: seconds?

Class template std::chrono::duration represents a time interval. It consists of a count of ticks of type Rep and a tick period, where the tick period is a compile-time rational fraction representing the time in seconds from one tick to the next.


1 Answers

Is this what you're looking for?

#include <chrono> #include <iostream>  int main() {     typedef std::chrono::high_resolution_clock Time;     typedef std::chrono::milliseconds ms;     typedef std::chrono::duration<float> fsec;     auto t0 = Time::now();     auto t1 = Time::now();     fsec fs = t1 - t0;     ms d = std::chrono::duration_cast<ms>(fs);     std::cout << fs.count() << "s\n";     std::cout << d.count() << "ms\n"; } 

which for me prints out:

6.5e-08s 0ms 
like image 105
Howard Hinnant Avatar answered Sep 19 '22 06:09

Howard Hinnant