std::chrono::duration

From Cppreference

Jump to: navigation, search
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

(constructor)
constructs new duration
(public member function)
(destructor)
destructs a duration
(public member function)
operator=
assigns the contents
(public member function)
count
returns the count of ticks
(public member function)
zero
returns the special duration value zero
(public member function)
min
returns the special duration value min
(public member function)
max
returns the special duration value max
(public member function)
operator+
operator-
implements unary + and unary -
(public member function)
operator++
operator++(int)
operator--
operator--(int)
increments or decrements the tick count
(public member function)
operator+=
operator-=
operator*=
operator/=
operator%=
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

std::common_type<std::chrono::duration>
specializes the std::common_type trait
(class template specialization)
operator+
operator-
operator*
operator/
operator%
implements arithmetic operations between two durations
(public member function)
operator==
operator!=
operator<
operator<=
operator>
operator>=
compares two durations
(public member function)
duration_cast
converts a duration to another, with a different tick interval
(public member function)

[edit] Helper classes

treat_as_floating_point
indicates that a duration is convertible to duration with different tick period
(class template)
duration_values
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