1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Measure the current time using different clocks.
//!
//! The following list of clocks are implemented.
//! * [`SystemClock`]: It provides access to the current wall-clock time from the system-wide
//! real-time clock.
//! * [`SteadyClock`]: It provides access to system-wide steady clock. There is no fixed
//! relationship between values returned by `SteadyClock::now()` and wall-clock time
//! * [`HighResolutionClock`]: Default to [`SteadyClock`] if available, otherwise fallback to
//! [`SystemClock`].
//! * [`ProcessRealCPUClock`]: It provides access to the real process wall-clock steady clock,
//! i.e. the real CPU-time clock of the calling process.
//! * [`ProcessUserCPUClock`]: It provides access to the user CPU-time steady clock of the
//! calling process.
//! * [`ProcessSystemCPUClock`]: It provides access to the system CPU-time steady clock of
//! the calling process.
//! * [`ProcessCPUClock`]: It provides access to real, user-CPU, and system-CPU clocks at
//! the same time.
//! * [`ThreadClock`]: It provides access to the real thread wall-clock, i.e. the real CPU-time
//! clock of the calling thread.
//!
//! # Implementations
//!
//! The Implementations of the clocks are based on
//! [`boost-chrono` library](https://boost.org/libs/chrono).
//! The following table listes the underlying APIs for different clocks.
//!
//! | Clock | Posix | Darwin | Windows |
//! |-------|-------|--------|---------|
//! | [`SystemClock`] | `clock_gettime(CLOCK_REALTIME)` | `gettimeofday` | `GetSystemTimeAsFileTime` |
//! | [`SteadyClock`] | `clock_gettime(CLOCK_MONOTONIC)` | `mach_timebase_info`, `mach_absolute_time` | `QueryPerformanceCounter`, `QueryPerformanceFrequency` |
//! | [`ProcessRealCPUClock`] | `times` | `times` | `QueryPerformanceCounter`, `QueryPerformanceFrequency` |
//! | [`ProcessUserCPUClock`] | `times` | `times` | `GetProcessTimes` |
//! | [`ProcessSystemCPUClock`] | `times` | `times` | `GetProcessTimes` |
//! | [`ProcessCPUClock`] | `times` | `times` | `GetProcessTimes`, `QueryPerformanceCounter`, `QueryPerformanceFrequency` |
//! | [`ThreadClock`] | `clock_gettime(pthread_getcpuclockid)` | `thread_info` | `GetThreadTimes` |
//!
//! # Examples
//!
//! ```
//! use howlong::*;
//!
//! let start = HighResolutionClock::now();
//! // do some computations
//! let elapsed = HighResolutionClock::now() - start;
//! println!("{:?} have passed.", elapsed);
//!
//! let start = ProcessCPUClock::now();
//! // do other computations
//! let elapsed = ProcessCPUClock::now() - start;
//! println!(
//! "We spent {:?} user, {:?} system, {:?} total.",
//! elapsed.user, elapsed.system, elapsed.real
//! );
//! ```
cfg_if!
cfg_if!