std::chrono::duration
From Cppreference
C++ Standard Library | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Utilities library | ||||||||||||||||||||||||||||||||||||||||
Date and time utilities | ||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||
Defined in header <chrono>
|
||
Defined in namespace std::chrono
| ||
template <class Rep, class Period = std::ratio<1> >
class duration; |
(C++11 feature) | |
Class template std::duration represents a time interval. It is represented by a count of ticks and a tick period, where the tick period is the number of seconds from one tick to the next, represented as a compile-time rational constant.
Contents |
[edit] Member types
Member type | Definition |
rep | Rep, an arithmetic type representing the number of ticks |
period | Period, an std::ratio type representing the tick period |
[edit] Member functions
|
constructs new duration (public member function) |
|
|
destructs a duration (public member function) |
|
|
assigns the contents (public member function) |
|
|
returns the count of ticks (public member function) |
|
|
returns the special duration value zero (public member function) |
|
|
returns the special duration value min (public member function) |
|
|
returns the special duration value max (public member function) |
|
|
implements unary + and unary - (public member function) |
|
|
increments or decrements the tick count (public member function) |
|
|
implements compound assignment between two durations (public member function) |
[edit] Non-member types
Type | Definition |
nanoseconds | duration type with Period std::nano |
microseconds | duration type with Period std::micro |
milliseconds | duration type with Period std::milli |
seconds | duration type with Period std::ratio<1> |
minutes | duration type with Period std::ratio<60> |
hours | duration type with Period std::ratio<3600> |
[edit] Non-member functions
|
specializes the std::common_type trait (class template specialization) |
|
|
implements arithmetic operations between two durations (public member function) |
|
|
compares two durations (public member function) |
|
|
converts a duration to another, with a different tick interval (public member function) |
[edit] Helper classes
|
indicates that a duration is convertible to duration with different tick period (class template) |
|
|
constructs zero, min, and max values of a tick count of given type (class template) |
[edit] Example
This example measures the execution time of a function
#include <iostream> #include <chrono> #include <thread> void f() { std::this_thread::sleep_for(std::chrono::seconds(1)); } int main() { auto t1 = std::chrono::high_resolution_clock::now(); f(); auto t2 = std::chrono::high_resolution_clock::now(); std::cout << "f() took " << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count() << " milliseconds\n"; }
Output:
f() took 1000 milliseconds