d4458bae08
Some checks failed
timw4mail/rusty-numbers/pipeline/head There was a failure building this commit
44 lines
1.5 KiB
Rust
44 lines
1.5 KiB
Rust
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
|
|
use rusty_numbers::{factorial, fibonacci, it_factorial, mem_fibonacci, rec_fibonacci};
|
|
|
|
fn bench_factorial(c: &mut Criterion) {
|
|
let mut group = c.benchmark_group("Factorials");
|
|
|
|
for i in [0usize, 5, 10, 15, 20, 25, 30, 34].iter() {
|
|
group.bench_with_input(BenchmarkId::new("Recursive naive", i), i, |b, i| {
|
|
b.iter(|| factorial(*i))
|
|
});
|
|
group.bench_with_input(BenchmarkId::new("Iterative", i), i, |b, i| {
|
|
b.iter(|| it_factorial(*i))
|
|
});
|
|
}
|
|
group.finish();
|
|
}
|
|
|
|
fn bench_fibonacci(c: &mut Criterion) {
|
|
let mut group = c.benchmark_group("Fibonacci");
|
|
|
|
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))
|
|
});
|
|
}
|
|
|
|
group.finish();
|
|
|
|
let mut group = c.benchmark_group("Recursive Fibonacci");
|
|
for i in [0usize, 5, 10, 15, 20, 25, 26, 27, 28, 29, 30].iter() {
|
|
group.bench_with_input(BenchmarkId::new("Naive Recursive", i), i, |b, i| {
|
|
b.iter(|| rec_fibonacci(*i))
|
|
});
|
|
}
|
|
group.finish();
|
|
}
|
|
|
|
criterion_group!(benches, bench_factorial, bench_fibonacci);
|
|
criterion_main!(benches);
|