Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::chrono::seconds lightweight? [closed]

Tags:

c++

c++11

For a embedded project I'd like to use std::chrono::seconds to represent timeout values, especially for the reason of typesaftyness. This requires std::chrono::seconds to be fast constructible from an unsigned int type and also passing by value has to be very fast. Is that the case?

like image 254
TNA Avatar asked Feb 07 '14 11:02

TNA


People also ask

What is std :: Chrono :: seconds?

std::chrono::seconds Instantiation of duration to represent seconds.

What is std :: Chrono :: 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 fraction representing the time in seconds from one tick to the next. The only data stored in a duration is a tick count of type Rep .

What is Chrono used for C++?

Chrono in C++ Chrono library is used to deal with date and time. This library was designed to deal with the fact that timers and clocks might be different on different systems and thus to improve over time in terms of precision.


1 Answers

Update. It seems that I can be bothered to check the standard sometimes.

Here's what the standard says about copy construction of duration.

20.11.5.1 duration constructors

template <class Rep2, class Period2>
constexpr duration(const duration<Rep2, Period2>& d);

Effects: Constructs an object of type duration, constructing rep_ from

duration_cast<duration>(d).count()

20.11.5.7 duration_cast

template <class ToDuration, class Rep, class Period>
constexpr ToDuration duration_cast(const duration<Rep, Period>& d);

2 Returns: Let CF be

ratio_divide<Period, typename ToDuration::period>

— If CF::num == 1 and CF::den == 1, returns

ToDuration(static_cast<typename ToDuration::rep>(d.count()))

20.11.5.2 duration observer

constexpr rep count() const; 1 Returns: rep_.

From all this, unless I'm mistaken, copy constructor should get the rep from original, static_casts it to new rep type (which is the same in this case) and constructs the new duration object with the rep constructor. At least, in effect. If the implementation does something incredibly heavy in addition to that, consider throwing it away.

Unfortunately, I couldn't find limitations to member data in the standard but the document linked by Howard Hinnant is probably more credible source than cppreference.com and it states:

The representation stores a count of ticks. This count is the only data member stored in a duration

As nice as this sounds, if the performance matters, profile your code. If it doesn't matter, this question is pointless.

Old answer...

According to cppreference.com

The only data stored in a duration is a tick count of type Rep.

So, yes, it seems to be fast for value passing. And as I suggested in the comment, std::chrono::duration<unsigned int> should be slightly more efficient and you shouldn't then need to worry about overflows as long as the original unsigned int doesn't overflow.

like image 59
eerorika Avatar answered Sep 19 '22 04:09

eerorika