42 lines
1.3 KiB
Rust
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);
|