Libqi provides types to model clocks, time points and durations. The implementation is based on boost::chrono. You can thus refer to boost::chrono documentation or to your favourite C++11 book for a detailed description. Here is a short introduction to the concepts though.
Warning
While a default-constructed time point has a well-defined value (equal to its clock epoch), a default-constructed duration is not required to be zero-initialized.
libqi exposes several clocks, each of which having a distinctive set of properties.
Possible properties are:
A clock is monotonic if the time points of this clock cannot decrease as physical time moves forward.
A clock is steady if the physical time between its ticks is constant. This property implies monotonicity.
Libqi provides three clocks:
qi::SteadyClock
qi::Clock
qi::SystemClock
Let’s use libqi to time some long computation and display it duration in milliseconds.
#include <qi/clock.hpp>
#include <iostream>
void long_computation() {
// ...
}
int main() {
qi::SteadyClock::time_point start = qi::SteadyClock::now();
long_computation();
qi::SteadyClock::time_point end = qi::SteadyClock::now();
qi::MilliSeconds ms = boost::chrono::duration_cast<qi::MilliSeconds>(end - start);
std::cout << "spent " << qi::to_string(ms) << std::endl;
return 0;
}
Or a slightly shorter version
#include <qi/clock.hpp>
#include <iostream>
void long_computation() {
// ...
}
int main() {
qi::SteadyClock::time_point start = qi::SteadyClock::now();
long_computation();
std::cout << "spent " << qi::to_string(qi::durationSince<qi::MilliSeconds>(start)) << std::endl;
return 0;
}
libqi heavily uses boost::chrono but does not directly expose the boost::chrono clocks. There are several reasons for this choice:
Global Namespaces
namespace qi
Classes (namespace qi)
class qi::SteadyClock
Functions (class qi::SteadyClock)
Members (class qi::SteadyClock)
class qi::SystemClock
Functions (class qi::SystemClock)
Members (class qi::SystemClock)
class qi::Clock
Functions (class qi::Clock)
Members (class qi::Clock)
Functions (namespace qi)
Global Members
qi::NanoSeconds
Duration¶Expect
¶Brief: Enum expected argument.
Name | Brief |
---|---|
Expect_SoonerOrLater |
Pick the nearest result to user-provided reference. |
Expect_Later |
Result is expected to be later than user-provided reference. |
Expect_Sooner |
Result is expected to be sooner than user-provided reference. |
time_point
is_steady
qi::Duration
duration¶The time points of this clock cannot decrease as physical time moves forward. This clock is not related to wall clock time, and is best suitable for measuring intervals.
time_point
¶qi::SteadyClock::
is_steady
¶true if t1 <= t2 is always true, else false. A clock that can be adjusted backwards is not steady.
qi::SteadyClock::
now
()¶Returns a time_point representing the current value of the clock.
The Clock class represents a system-wide clock, best suitable for timestamping events. Typically monotonic and unaffected by the system clock adjustment, altough this is not guaranteed. More...
#include <qi/clock.hpp>
Expect
¶Brief: Enum expected argument.
Name | Brief |
---|---|
Expect_SoonerOrLater |
Pick the nearest result to user-provided reference. |
Expect_Later |
Result is expected to be later than user-provided reference. |
Expect_Sooner |
Result is expected to be sooner than user-provided reference. |
now
()toUint32ms
(const time_point& t)toInt32ms
(const time_point& t)fromUint32ms
(uint32_t t_ms, time_point guess, Expect expect)fromInt32ms
(int32_t t_ms, time_point guess, Expect expect)qi::Duration
duration¶qi::Clock::
is_steady
¶true if t1 <= t2 is always true, else false. A clock that can be adjusted backwards is not steady.
qi::Clock::
now
()¶Returns a time_point representing the current value of the clock.
qi::Clock::
toUint32ms
(const time_point& t)¶Brief: Convert the time point to a number of milliseconds on 32 bits.
Parameters: |
|
---|---|
Returns: | Unsigned int representing the time. |
Since the 32 bits number overflows every 2^32 ms ~ 50 days, this is a lossy operation.
qi::Clock::
toInt32ms
(const time_point& t)¶Brief: Convert the time point to a number of milliseconds on 32 bits.
Parameters: |
|
---|---|
Returns: | Integer (int) representing the time. |
Since the 32 bits number overflows every 2^32 ms ~ 50 days, this is a lossy operation.
qi::Clock::
fromUint32ms
(uint32_t t_ms, time_point guess, Expect expect = Expect_SoonerOrLater)¶Since the 32 bits number overflows every ~50 days, an infinity of time points match a given 32 bits number (all modulo ~50 days). This function picks the result near the guess timepoint depending on the expect argument:
if expect == LATER, result is expected to be later than guess: guess <= result < guess + periodif expect == SOONER, result is expected to be sooner than guess: guess - period < result <= guessif expect == SOONER_OR_LATER, pick the nearest result: guess - period/2 < result <= guess + period/2
where period == 2^32 ms ~ 50 days
qi::Clock::
fromInt32ms
(int32_t t_ms, time_point guess, Expect expect = Expect_SoonerOrLater)¶Since the 32 bits number overflows every ~50 days, an infinity of time points match a given 32 bits number (all modulo ~50 days). This function picks the result near the guess timepoint depending on the expect argument:
if expect == LATER, result is expected to be later than guess: guess <= result < guess + periodif expect == SOONER, result is expected to be sooner than guess: guess - period < result <= guessif expect == SOONER_OR_LATER, pick the nearest result: guess - period/2 < result <= guess + period/2
where period == 2^32 ms ~ 50 days
The SystemClock class represents the system-wide real time wall clock. It may not be monotonic: on most systems, the system time can be adjusted at any moment. More...
#include <qi/clock.hpp>
time_point
is_steady
now
()to_time_t
(const time_point& t)from_time_t
(const std::time_t& t)qi::Duration
duration¶time_point
qi::SystemClock::
is_steady
¶true if t1 <= t2 is always true, else false. A SystemClock is never steady.
qi::SystemClock::
now
()¶Returns a time_point representing the current value of the clock.
qi::SystemClock::
to_time_t
(const time_point& t)¶Brief: Converts a system clock time point to std::time_t.
Parameters: |
|
---|---|
Returns: | A std::time_t representing t. |
qi::SystemClock::
from_time_t
(const std::time_t& t)¶Brief: Converts std::time_t to a system clock time point.
Parameters: |
|
---|---|
Returns: | A time point representing t. |
qi::SteadyClock::time_point
SteadyClockTimePoint¶qi::Clock::time_point
ClockTimePoint¶qi::SystemClock::time_point
SystemClockTimePoint¶qi::
sleepFor
(const qi::Duration& d)¶Blocks the execution of the current thread for at least d.
qi::
sleepUntil
(const SteadyClockTimePoint& t)¶This is equivalent to sleepFor(t - SteadyClockTimePoint::now())
qi::
sleepUntil
(const ClockTimePoint& t)¶Blocks the execution of the current thread until t has been reached.
qi::
sleepUntil
(const SystemClockTimePoint& t)¶Adjustments of the clock are taken into account. Thus the duration of the block might, but might not, be less or more than t - SystemClock::now()
qi::
toISO8601String
(const SystemClockTimePoint& t)¶For instance the string for a quarter past nine PM on April 3rd, 2001 is “2001-04-03T211500.000Z”
qi::
durationSince
(const TimePointFrom& t)¶Returns the duration elapsed since t.