5

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?

Narek
  • 38,779
  • 79
  • 233
  • 389
  • 2
    [`std::chrono::duration::count()`](http://en.cppreference.com/w/cpp/chrono/duration/count). – T.C. Jul 10 '14 at 21:43
  • 3
    Whatever happened to [reading the documentation](http://en.cppreference.com/w/cpp/chrono/duration) [before](http://meta.stackoverflow.com/a/261593/596781) posting on SO? – Kerrek SB Jul 10 '14 at 21:44
  • 5
    I have read, but sorry "returns the count of ticks" tells me nothing. – Narek Jul 10 '14 at 21:47
  • You should avoid using weak types like `float` or `long long` to represent a duration. Instead you should stick with strong types, such as `std::chrono::milliseconds` or an appropriate specialization of `std::chrono::duration`. – bames53 Jul 10 '14 at 21:49
  • @bames53 I need to multiply the coordinate with a float, how I should do that without conversion? – Narek Jul 10 '14 at 21:50
  • What exactly are you calculating? – bames53 Jul 10 '14 at 21:53
  • @bames53 some physical calculation which depends of time duration. :) – Narek Jul 10 '14 at 22:03
  • 1
    @Narek is the result of the calculation a quantity of time? Is it unitless? Is it a quantity of distance? – bames53 Jul 10 '14 at 22:19
  • 1
    @Narek: Sounds like you want to multiply a velocity and a time and get a distance, which you then add to a location to get a new location to represent movement? Notice how that question doesn't contain the word `float`, or even "scalar"? `location += velocity * duration;` – Mooing Duck Jul 10 '14 at 22:23
  • @MooingDuck I don't get you. I have tried to multiply or divide and got error: `could not deduce template argument for 'const std::chrono::duration<_Rep,_Period> &' from 'float32'` – Narek Jul 10 '14 at 22:39
  • You said you are multiplying a "coordinate" with a "float". What does the coordinate represent, and what is it's exact type? I assume the float is the `std::chrono::milliseconds`. What does the result represent? – Mooing Duck Jul 10 '14 at 22:49

2 Answers2

8

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.

Community
  • 1
  • 1
bames53
  • 86,085
  • 15
  • 179
  • 244
0

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
}
Akira Takahashi
  • 2,912
  • 22
  • 107