Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Other Clocks

Header <boost/chrono/process_cpu_clocks.hpp>
Macro BOOST_CHRONO_HAS_PROCESS_CLOCKS
Class process_real_cpu_clock
Class process_user_cpu_clock
Class process_system_cpu_clock
Class process_cpu_clock
Template Class process_times
process_times Input/Output
duration_values Specialization for process_times<>
clock_string<process_real_cpu_clock> Specialization
clock_string<process_user_cpu_clock> Specialization
clock_string<process_system_cpu_clock> Specialization
clock_string<process_cpu_clock> Specialization
numeric_limits Specialization for process_times<>
Header <boost/chrono/thread_clock.hpp>
Macro BOOST_CHRONO_HAS_THREAD_CLOCK
Macro BOOST_CHRONO_THREAD_CLOCK_IS_STEADY
Class thread_clock
clock_string<thread_clock> Specialization

Knowing how long a program takes to execute is useful in both test and production environments. It is also helpful if such timing information is broken down into real (wall clock) time, CPU time spent by the user, and CPU time spent by the operating system servicing user requests.

Process clocks don't include the time spent by the child process.

#define BOOST_CHRONO_HAS_PROCESS_CLOCKS

namespace boost { namespace chrono {

    class process_real_cpu_clock;
    class process_user_cpu_clock;
    class process_system_cpu_clock;
    class process_cpu_clock;

    template <typename Rep>
    struct process_times;
    template <class CharT, class Traits, class Rep>
    std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os,
            process_times<Rep> const& rhs);

    template <class CharT, class Traits, class Rep>
    std::basic_istream<CharT, Traits>&
    operator>>(std::basic_istream<CharT, Traits>& is,
            process_times<Rep> const& rhs);

    template <class Rep>
    struct duration_values<process_times<Rep> >;

    template <class CharT>
    struct clock_string<process_real_cpu_clock, CharT>;
    struct clock_string<process_user_cpu_clock, CharT>;
    struct clock_string<process_system_cpu_clock, CharT>;
    struct clock_string<process_cpu_clock, CharT>;

} }
namespace std {
    template <class Rep>
    class numeric_limits<boost::chrono::process_times<Rep> >;
}

This macro is defined if the platform supports process clocks.

process_real_cpu_clock satisfy the Clock requirements.

process_real_cpu_clock class provides access to the real process wall-clock steady clock, i.e. the real CPU-time clock of the calling process. The process relative current time can be obtained by calling process_real_cpu_clock::now().

class process_real_cpu_clock {
public:
    typedef nanoseconds                          duration;
    typedef duration::rep                        rep;
    typedef duration::period                     period;
    typedef chrono::time_point<process_real_cpu_clock>    time_point;
    static constexpr bool is_steady =            true;

    static time_point now(  ) noexcept;
    static time_point now( system::error_code & ec );
};

process_user_cpu_clock satisfy the Clock requirements.

process_user_cpu_clock class provides access to the user CPU-time steady clock of the calling process. The process relative user current time can be obtained by calling process_user_cpu_clock::now().

class process_user_cpu_clock {
public:
    typedef nanoseconds                          duration;
    typedef duration::rep                        rep;
    typedef duration::period                     period;
    typedef chrono::time_point<process_user_cpu_clock>    time_point;
    static constexpr bool is_steady =            true;

    static time_point now(  ) noexcept;
    static time_point now( system::error_code & ec );
};

process_system_cpu_clock satisfy the Clock requirements.

process_system_cpu_clock class provides access to the system CPU-time steady clock of the calling process. The process relative system current time can be obtained by calling process_system_cpu_clock::now().

class process_system_cpu_clock {
public:
    typedef nanoseconds                          duration;
    typedef duration::rep                        rep;
    typedef duration::period                     period;
    typedef chrono::time_point<process_system_cpu_clock>    time_point;
    static constexpr bool is_steady =            true;

    static time_point now(  ) noexcept;
    static time_point now( system::error_code & ec );
};

process_cpu_clock can be considered as a tuple<process_real_cpu_clock, process_user_cpu_clock, process_system_cpu_clock>.

process_cpu_clock provides a thin wrapper around the operating system's process time API. For POSIX-like systems, that's the times() function, while for Windows, it's the GetProcessTimes() function.

The process relative real, user and system current time can be obtained at once by calling process_clocks::now().

class process_cpu_clock
{
public:
    typedef process_times<nanoseconds::rep> times ;

    typedef duration<times,  nano>                  duration;
    typedef duration::rep                           rep;
    typedef duration::period                        period;
    typedef chrono::time_point<process_cpu_clock>   time_point;
    static constexpr bool is_steady =               true;

    static time_point now(  ) noexcept;
    static time_point now( system::error_code & ec );
};

This class is the representation of the process_cpu_clock::duration class. As such it needs to implements the arithmetic operators.

template <typename Rep>
struct process_times : arithmetic<process_times<Rep>,
    multiplicative<process_times<Rep>, Rep,
    less_than_comparable<process_times<Rep> > > >
{
    Rep real;    // real (i.e wall clock) time
    Rep user;    // user cpu time
    Rep system;  // system cpu time

    times();
    times(
        process_real_cpu_clock::rep r,
        process_user_cpu_clock::rep u,
        process_system_cpu_clock::rep s);

    template <typename Rep2>
    explicit process_times(
        Rep2 r);
    template <typename Rep2>
    explicit process_times(
        process_times<Rep2> const& rhs);
    operator rep() const;

    bool operator==(process_times const& rhs);
    template <typename Rep2>
    bool operator==(process_times<Rep2> const& rhs);

    times operator+=(process_times const& rhs);
    times operator-=(process_times const& rhs);
    times operator*=(process_times const& rhs);
    times operator/=(process_times const& rhs);
    bool operator<(process_times const & rhs) const;
};
template <class CharT, class Traits, class Rep>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os,
        process_times<Rep> const& rhs);

Effects: Output each part separated by ';' and surrounded by '{', '}'.

Throws: None.

template <class CharT, class Traits, class Rep>
std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is,
        process_times<Rep> const& rhs);

Effects: overrides the value of rhs if the input stream has the format "{r;u;s}". Otherwise, set the input stream state as failbit | eofbit.

Throws: None.

template <class Rep>
struct duration_values<process_times<Rep> >
{
    static process_times<Rep> zero();
    static process_times<Rep> max();
    static process_times<Rep> min();
};

The times specific functions zero(), max() and min() uses the relative functions on the representation of each component.

template <class CharT>
struct clock_string<process_real_cpu_clock, CharT>
{
    static std::basic_string<CharT> name();
    static std::basic_string<CharT> since();
};

clock_string<>::name() returns "process_real_cpu_clock".

clock_string<>::since() returns " since process start-up"

template <class CharT>
struct clock_string<process_user_cpu_clock, CharT>
{
    static std::basic_string<CharT> name();
    static std::basic_string<CharT> since();
};

clock_string<>::name() returns "process_user_cpu_clock".

clock_string<>::since() returns " since process start-up"

template <class CharT>
struct clock_string<process_system_cpu_clock, CharT>
{
    static std::basic_string<CharT> name();
    static std::basic_string<CharT> since();
};

clock_string<>::name() returns "process_system_cpu_clock".

clock_string<>::since() returns " since process start-up"

template <class CharT>
struct clock_string<process_cpu_clock, CharT>
{
    static std::basic_string<CharT> name();
    static std::basic_string<CharT> since();
};

clock_string<>::name() returns "process_cpu_clock".

clock_string<>::since() returns " since process start-up"

namespace std {
    template <>
    class numeric_limits<boost::chrono::process_times<Rep>> {
        typedef boost::chrono::process_times<Rep> Res;

    public:
        static const bool is_specialized = true;
        static Res min();
        static Res max();
        static Res lowest();
        static const int digits;
        static const int digits10;
        static const bool is_signed = false;
        static const bool is_integer = true;
        static const bool is_exact = true;
        static const int radix = 0;
    };
}

The process_times<Rep> specialization functions min(), max() and lowest() uses the relative functions on the representation of each component.

Notes

  • min() returns the tuple of mins.
  • max() returns the tuple of maxs.
  • lowest() returns the tuple of lowests.
  • digits is the sum of (binary) digits.
  • digits10 is the sum of digits10s.

Knowing the time a thread takes to execute is useful in both test and production environments.

#define BOOST_CHRONO_HAS_THREAD_CLOCK
#define BOOST_CHRONO_THREAD_CLOCK_IS_STEADY
namespace boost { namespace chrono {

    class thread_clock;
    template <class CharT>
    struct clock_string<thread_clock, CharT>;

} }

This macro is defined if the platform supports thread clocks.

This macro is defined if the platform has a thread clock. Its value is true if it is steady and false otherwise.

thread_clock satisfy the Clock requirements.

thread_clock class provides access to the real thread wall-clock, i.e. the real CPU-time clock of the calling thread. The thread relative current time can be obtained by calling thread_clock::now().

class thread_clock {
public:
    typedef nanoseconds                          duration;
    typedef duration::rep                        rep;
    typedef duration::period                     period;
    typedef chrono::time_point<thread_clock>     time_point;
    static constexpr bool is_steady =            BOOST_CHRONO_THREAD_CLOCK_IS_STEADY;

    static time_point now(  ) noexcept;
    static time_point now( system::error_code & ec );
};
#if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
template <class CharT>
struct clock_string<thread_clock, CharT>
{
    static std::basic_string<CharT> name();
    static std::basic_string<CharT> since();
};
#endif

clock_string<>::name() returns "thread_clock".

clock_string<>::since() returns " since thread start-up"


PrevUpHomeNext