C++ chrono - get duration as float or long long

∥☆過路亽.° 提交于 2020-01-22 23:01:47

问题


I have a duration

typedef std::chrono::high_resolution_clock Clock;
Clock::time_point       beginTime;
Clock::time_point       endTime;
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - beginTime);

And I get duration in std::chrono::milliseconds. But I need duration as float or long long. How to do that?


回答1:


From the documentation

template<
    class Rep, 
    class Period = std::ratio<1> 
> class duration;

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 constant representing the number of seconds from one tick to the next.

And:

count returns the count of ticks

So a duration stores a number of ticks of a specified period of time, and count will return that number using the underlying representation type. So if the duration's representation is long long, and the period is std::milli, then .count() will return a long long equal to the number of milliseconds represented by the duration.


In general you should avoid using weak types like float or long long to represent a duration. Instead you should stick with 'rich' types, such as std::chrono::milliseconds or an appropriate specialization of std::chrono::duration. These types aid correct usage and readability, and help prevent mistakes via type checking.

  • Underspecified / overly general:
    – void increase_speed(double);
    – Object obj; … obj.draw();
    – Rectangle(int,int,int,int);

  • Better: – void increase_speed(Speed);
    – Shape& s; … s.draw();
    – Rectangle(Point top_left, Point bottom_right);
    – Rectangle(Point top_left, Box_hw b);

— slide 18 from Bjarne's talk


std::chrono is "a consistent subset of a physical quantities library that handles only units of time and only those units of time with exponents equal to 0 and 1."

If you need to work with quantities of time you should take advantage of this library, or one that provides more complete unit systems, such as boost::units.

There are rare occasions where quantities must be degraded to weakly typed values. For example, when one must use an API that requires such types. Otherwise it should be avoided.




回答2:


As float answer.

std::chrono's duration typedefs are integer. However, duration class can accept float.

See my duration typedefs:

https://github.com/faithandbrave/Shand/blob/master/shand/duration.hpp

...
template <class Rep>
using seconds_t = std::chrono::duration<Rep>;
using seconds_f = seconds_t<float>;
using seconds_d = seconds_t<double>;
using seconds_ld = seconds_t<long double>;

template <class Rep>
using minutes_t =  std::chrono::duration<Rep, std::ratio<60>>;
using minutes_f = minutes_t<float>;
using minutes_d = minutes_t<double>;
using minutes_ld = minutes_t<long double>;
...

These durations usage is here:

#include <iostream>
#include <shand/duration.hpp>

int main()
{
    std::chrono::seconds int_s(3);
    shand::minutes_f float_m = int_s; // without `duration_cast`

    std::cout << float_m.count() << std::endl; // 0.05
}


来源:https://stackoverflow.com/questions/24686781/c-chrono-get-duration-as-float-or-long-long

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!