Skip to content

Commit f324601

Browse files
committed
Doc
1 parent 663561c commit f324601

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ let stop = p.now();
2222
let elapsed2 = stop - start;
2323

2424
let elapsed_total = elapsed1 + elapsed2;
25-
let elapsed_total_secs = elapsed_total_as_secs_f64();
25+
let elapsed_total_secs = elapsed_total.as_secs_f64(&p);
2626
let hw_ticks = elapsed_total.ticks();
2727
```

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ impl Default for Config {
1414
}
1515

1616
impl Config {
17+
/// Sets the duration of the calibration, required to estimate the frequency
18+
/// of the hardware counter on Linux. Default is 5 seconds.
1719
pub fn setup_duration(mut self, setup_duration: Duration) -> Self {
1820
self.setup_duration = setup_duration;
1921
self

src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
#![cfg_attr(asm, feature(asm))]
22

3+
/// Precision is a simple crate to perform measurements using hardware counters.
4+
///
5+
/// It is especially useful for performing micro-benchmarks.
6+
///
7+
/// Example
8+
/// ```rust
9+
/// extern crate precision;
10+
///
11+
/// let p = precision::Precision::new(precision::Config::default()).unwrap();
12+
///
13+
/// let start = p.now();
14+
///
15+
/// let stop = p.now();
16+
/// let elapsed1 = stop - start;
17+
///
18+
/// let start = p.now();
19+
/// let stop = p.now();
20+
/// let elapsed2 = stop - start;
21+
///
22+
/// let elapsed_total = elapsed1 + elapsed2;
23+
/// let elapsed_total_secs = elapsed_total.as_secs_f64(&p);
24+
/// let hw_ticks = elapsed_total.ticks();
25+
/// ```
326
extern crate libc;
427

528
mod config;

src/precision.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ pub struct Precision {
1010
}
1111

1212
impl Precision {
13+
/// Initialize the crate. Note that on Linux system, this will
14+
/// perform calibration before returning. You may want to do this
15+
/// only twice. The `Precision` value can then be cloned if needed.
1316
pub fn new(config: Config) -> Result<Self, &'static str> {
1417
let frequency = Precision::guess_frequency(&config)?;
1518
Ok(Precision { frequency })
1619
}
1720

21+
/// Returns the current timestamp
1822
#[inline]
1923
pub fn now(&self) -> Timestamp {
2024
CPUCounter::current()

src/timestamp.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use super::precision::*;
22
use std::ops::*;
33

4+
/// A timestamp. Note that this is an opaque structure.
45
#[derive(Clone, Copy, Eq, PartialEq)]
56
pub struct Timestamp(pub(crate) u64);
67

8+
/// The difference between two timestamps.
79
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
810
pub struct Elapsed(u64);
911

@@ -37,36 +39,43 @@ impl AddAssign for Elapsed {
3739
}
3840

3941
impl Elapsed {
42+
/// Returns a nul duration
4043
#[inline]
4144
pub fn new() -> Self {
4245
Elapsed(0)
4346
}
4447

48+
/// Builds a `Duration` from a number of ticks
4549
#[inline]
4650
pub fn from_ticks(ticks: u64) -> Self {
4751
Elapsed(ticks)
4852
}
4953

54+
/// Returns the number of ticks for the given duration
5055
#[inline]
5156
pub fn ticks(&self) -> u64 {
5257
self.0
5358
}
5459

60+
/// Returns the duration as a number of seconds
5561
#[inline]
5662
pub fn as_secs(&self, precision: &Precision) -> u64 {
5763
self.0 / precision.frequency
5864
}
5965

66+
/// Returns the duration as a number of seconds (floating-point)
6067
#[inline]
6168
pub fn as_secs_f64(&self, precision: &Precision) -> f64 {
6269
self.0 as f64 / precision.frequency as f64
6370
}
6471

72+
/// Returns the duration as milliseconds
6573
#[inline]
6674
pub fn as_millis(&self, precision: &Precision) -> u64 {
6775
self.0 * 1_000 / precision.frequency
6876
}
6977

78+
/// Returns the duration as nanoseconds
7079
#[inline]
7180
pub fn as_ns(&self, precision: &Precision) -> u64 {
7281
(((self.0 as f64 * 1_000.0) / precision.frequency as f64) * 1_000_000.0) as u64

0 commit comments

Comments
 (0)