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
|
use std::time::Duration;
#[cfg(bench)]
use tiny_bench::{black_box, BenchmarkConfig};
fn main() {
#[cfg(bench)]
bench_test_one();
#[cfg(bench)]
bench_test_two();
#[cfg(bench)]
bench_test_three();
#[cfg(bench)]
bench_test_four();
}
#[cfg(bench)]
fn bench_test_one() {
tiny_bench::bench_labeled("test one", || {
let mut v: Vec<i32> = Vec::with_capacity(10_000);
for i in 0..black_box(10_000) {
v.push(black_box(i));
}
let mut sum = 0;
for i in black_box(v) {
sum += black_box(i);
}
assert!(sum >= black_box(1));
});
}
#[cfg(bench)]
fn bench_test_two() {
tiny_bench::bench_with_setup_labeled(
"test two",
|| {
std::thread::sleep(Duration::from_micros(1));
let mut v: Vec<i32> = Vec::with_capacity(10_000);
for i in 0..10_000 {
v.push(black_box(i));
}
v
},
|v| {
let mut sum = 0;
for i in black_box(v) {
sum += black_box(i);
}
assert!(sum >= black_box(1));
},
);
}
#[cfg(bench)]
fn bench_test_three() {
tiny_bench::bench_labeled("test three, empty", || {});
}
#[cfg(bench)]
fn bench_test_four() {
tiny_bench::bench_with_configuration_labeled(
"test four, max_it",
&BenchmarkConfig {
max_iterations: Some(5000),
..BenchmarkConfig::default()
},
|| {},
);
}
|