rusty-numbers/benches/stock_functions.rs
Timothy Warren 75fe940ffc
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good
Add benchmarks
2020-02-20 17:13:46 -05:00

42 lines
1.3 KiB
Rust

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rusty_numbers::{factorial, fibonacci, rec_factorial, rec_fibonacci};
fn bench_factorial(c: &mut Criterion) {
let mut group = c.benchmark_group("Factorials");
// Recursive with lookup table
group.bench_function("factorial", |b| b.iter(|| factorial(34)));
group.bench_function("factorial black box", |b| {
b.iter(|| factorial(black_box(34)))
});
// Naive recursive
group.bench_function("rec_factorial", |b| b.iter(|| rec_factorial(34)));
group.bench_function("rec_factorial black box", |b| {
b.iter(|| rec_factorial(black_box(34)))
});
group.finish();
}
fn bench_fibonacci(c: &mut Criterion) {
let mut group = c.benchmark_group("Fibonacci");
// Recursive with lookup table
group.bench_function("fibonacci", |b| b.iter(|| fibonacci(86)));
group.bench_function("fibonacci black box", |b| {
b.iter(|| fibonacci(black_box(86)))
});
// Naive recursive
group.bench_function("rec_fibonacci", |b| b.iter(|| rec_fibonacci(86)));
group.bench_function("rec_fibonacci black box", |b| {
b.iter(|| rec_fibonacci(black_box(86)))
});
group.finish();
}
criterion_group!(benches, bench_factorial, bench_fibonacci);
criterion_main!(benches);