2020-02-20 21:26:28 -05:00
|
|
|
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
|
|
|
|
use rusty_numbers::{factorial, fibonacci, mem_fibonacci, mem_factorial};
|
2020-02-20 17:13:46 -05:00
|
|
|
|
|
|
|
fn bench_factorial(c: &mut Criterion) {
|
|
|
|
let mut group = c.benchmark_group("Factorials");
|
|
|
|
|
2020-02-20 21:26:28 -05:00
|
|
|
for i in [0usize, 5, 10, 15, 20, 25, 30, 34].iter() {
|
|
|
|
group.bench_with_input(BenchmarkId::new("Recursive memoized", i), i, |b, i| {
|
|
|
|
b.iter(|| mem_factorial(*i))
|
|
|
|
});
|
|
|
|
group.bench_with_input(BenchmarkId::new("Recursive naive", i), i, |b, i| {
|
|
|
|
b.iter(|| factorial(*i))
|
|
|
|
});
|
|
|
|
}
|
2020-02-20 17:13:46 -05:00
|
|
|
group.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bench_fibonacci(c: &mut Criterion) {
|
|
|
|
let mut group = c.benchmark_group("Fibonacci");
|
|
|
|
|
2020-02-20 21:26:28 -05:00
|
|
|
for i in [0usize, 5, 10, 15, 20, 30].iter() {
|
|
|
|
group.bench_with_input(BenchmarkId::new("Recursive memoized", i), i, |b, i| {
|
|
|
|
b.iter(|| mem_fibonacci(*i))
|
|
|
|
});
|
|
|
|
// group.bench_with_input(BenchmarkId::new("Recursive naive", i), i, |b, i| b.iter(|| rec_fibonacci(*i)));
|
|
|
|
group.bench_with_input(BenchmarkId::new("Iterative", i), i, |b, i| {
|
|
|
|
b.iter(|| fibonacci(*i))
|
|
|
|
});
|
|
|
|
}
|
2020-02-20 17:13:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, bench_factorial, bench_fibonacci);
|
|
|
|
criterion_main!(benches);
|