proxygen
|
#include <stop_watch.h>
Public Types | |
using | clock_type = Clock |
using | duration = Duration |
using | time_point = std::chrono::time_point< clock_type, duration > |
Public Member Functions | |
custom_stop_watch () | |
custom_stop_watch (typename clock_type::time_point checkpoint) | |
void | reset () |
duration | elapsed () const |
template<typename UDuration > | |
bool | elapsed (UDuration &&amount) const |
duration | lap () |
template<typename UDuration > | |
bool | lap (UDuration &&amount) |
clock_type::time_point | getCheckpoint () const |
Private Attributes | |
clock_type::time_point | checkpoint_ |
Calculates the duration of time intervals. Prefer this over directly using monotonic clocks. It is very lightweight and provides convenient facilitles to avoid common pitfalls.
There are two type aliases that should be preferred over instantiating this class directly: coarse_stop_watch
and stop_watch
.
Arguments:
Example 1:
coarse_stop_watch<std::chrono::seconds> watch; do_something(); std::cout << "time elapsed: " << watch.elapsed().count() << std::endl;
auto const ttl = 60_s; if (watch.elapsed(ttl)) { process_expiration(); }
Example 2:
struct run_every_n_seconds { using callback = std::function<void()>; run_every_n_seconds(std::chrono::seconds period, callback action) period_(period), action_(std::move(action)) { // watch_ is correctly initialized to the current time }
void run() { while (true) { if (watch_.lap(period_)) { action_(); } std::this_thread::yield(); } }
private: stop_watch<> watch_; std::chrono::seconds period_; callback action_; };
Definition at line 83 of file stop_watch.h.
using folly::custom_stop_watch< Clock, Duration >::clock_type = Clock |
Definition at line 84 of file stop_watch.h.
using folly::custom_stop_watch< Clock, Duration >::duration = Duration |
Definition at line 85 of file stop_watch.h.
using folly::custom_stop_watch< Clock, Duration >::time_point = std::chrono::time_point<clock_type, duration> |
Definition at line 86 of file stop_watch.h.
|
inline |
Initializes the stop watch with the current time as its checkpoint.
Example:
stop_watch<> watch; do_something(); std::cout << "time elapsed: " << watch.elapsed() << std::endl;
Definition at line 109 of file stop_watch.h.
|
inlineexplicit |
Initializes the stop watch with the given time as its checkpoint.
NOTE: this constructor should be seldomly used. It is only provided so that, in the rare occasions it is needed, one does not have to reimplement the custom_stop_watch
class.
Example:
custom_stop_watch<monotonic_clock> watch(monotonic_clock::now()); do_something(); std::cout << "time elapsed: " << watch.elapsed() << std::endl;
Definition at line 126 of file stop_watch.h.
|
inline |
Tells the elapsed time since the last update.
The stop watch's checkpoint remains unchanged.
Example:
stop_watch<> watch; do_something(); std::cout << "time elapsed: " << watch.elapsed() << std::endl;
Definition at line 168 of file stop_watch.h.
References folly::custom_stop_watch< Clock, Duration >::checkpoint_, and now().
Referenced by testTryReadUntil(), and testTryWriteUntil().
|
inline |
Tells whether the given duration has already elapsed since the last checkpoint.
Example:
auto const ttl = 60_s; stop_watch<> watch;
do_something();
std::cout << "has the TTL expired? " std::boolalpha<< watch.elapsed(ttl);
Definition at line 189 of file stop_watch.h.
References folly::custom_stop_watch< Clock, Duration >::checkpoint_, and now().
|
inline |
Returns the current checkpoint
Definition at line 257 of file stop_watch.h.
References folly::custom_stop_watch< Clock, Duration >::checkpoint_.
Referenced by testTryReadUntil(), and testTryWriteUntil().
|
inline |
Tells the elapsed time since the last update, and updates the checkpoint to the current time.
Example:
struct some_resource { // ...
void on_reloaded() { auto const alive = time_alive.lap(); std::cout << "resource reloaded after being alive for " << alive; }
private: stop_watch<> time_alive; };
Definition at line 213 of file stop_watch.h.
References folly::custom_stop_watch< Clock, Duration >::checkpoint_, and now().
|
inline |
Tells whether the given duration has already elapsed since the last checkpoint. If so, update the checkpoint to the current time. If not, the checkpoint remains unchanged.
Example:
void run_every_n_seconds( std::chrono::seconds period, std::function<void()> action ) { for (stop_watch<> watch;; ) { if (watch.lap(period)) { action(); } std::this_thread::yield(); } }
Definition at line 243 of file stop_watch.h.
References folly::custom_stop_watch< Clock, Duration >::checkpoint_, and now().
|
inline |
Updates the stop watch checkpoint to the current time.
Example:
struct some_resource { // ...
void on_reloaded() { time_alive.reset(); }
void report() { std::cout << "resource has been alive for " << time_alive.elapsed(); }
private: stop_watch<> time_alive; };
Definition at line 151 of file stop_watch.h.
References folly::custom_stop_watch< Clock, Duration >::checkpoint_, and now().
|
private |