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
//! Flexible, stand-alone benchmarking.

#![deny(missing_docs)]

#![cfg_attr(asm, feature(asm))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(all(unix, feature = "std"))]
extern crate libc;

mod black_box;

#[cfg(feature = "std")] pub mod bencher;
#[cfg(feature = "std")] pub mod benchmark;
#[cfg(feature = "std")] pub mod reporter;
#[cfg(feature = "std")] pub mod runner;
#[cfg(feature = "std")] pub mod timer;

#[allow(missing_docs)]
pub mod no_std;

pub use self::black_box::black_box;

/// Data sample for a specific benchmark, parametric in the sample unit.
#[cfg(feature = "std")]
pub struct Sample<T> {
    /// Name of the benchmark.
    pub name: String,

    /// Recorded data.
    pub data: Vec<T>,
}

#[macro_export]
macro_rules! bench {
    ($name: ident, $body: expr) => {
        #[allow(non_camel_case_types)]
        struct $name;

        impl Benchmark<()> for $name {
            fn name(&self) -> String {
                stringify!($name).to_string()
            }

            fn setup(&mut self) {}

            fn target(&mut self) {
                black_box({
                    $body
                });
            }

            fn teardown(&mut self) {}
        }
    };
}

#[macro_export]
macro_rules! run_bench {
    ($b:ident, $name:ident) => {
        $b.run(&mut $name {});
    }
}