2020-02-20 21:26:28 -05:00
|
|
|
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
|
2020-02-21 16:33:00 -05:00
|
|
|
use rusty_numbers::num::Unsigned;
|
2020-02-21 13:15:05 -05:00
|
|
|
use rusty_numbers::{factorial, fibonacci, it_factorial, mem_fibonacci, rec_fibonacci};
|
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 naive", i), i, |b, i| {
|
2020-02-21 16:33:00 -05:00
|
|
|
b.iter(|| factorial(black_box(*i)))
|
2020-02-20 21:26:28 -05:00
|
|
|
});
|
2020-02-21 13:15:05 -05:00
|
|
|
group.bench_with_input(BenchmarkId::new("Iterative", i), i, |b, i| {
|
2020-02-21 16:33:00 -05:00
|
|
|
b.iter(|| it_factorial(black_box(*i)))
|
2020-02-21 13:15:05 -05:00
|
|
|
});
|
2020-02-20 21:26:28 -05:00
|
|
|
}
|
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-21 16:33:00 -05:00
|
|
|
for i in [0usize, 10, 20, 30, 40, 50, 70, 93, 140, 186].iter() {
|
2020-02-20 21:26:28 -05:00
|
|
|
group.bench_with_input(BenchmarkId::new("Recursive memoized", i), i, |b, i| {
|
2020-02-21 16:33:00 -05:00
|
|
|
b.iter(|| mem_fibonacci(black_box(*i)))
|
2020-02-20 21:26:28 -05:00
|
|
|
});
|
|
|
|
group.bench_with_input(BenchmarkId::new("Iterative", i), i, |b, i| {
|
2020-02-21 16:33:00 -05:00
|
|
|
b.iter(|| fibonacci(black_box(*i)))
|
2020-02-20 21:26:28 -05:00
|
|
|
});
|
|
|
|
}
|
2020-02-21 13:15:05 -05:00
|
|
|
|
|
|
|
group.finish();
|
|
|
|
|
|
|
|
let mut group = c.benchmark_group("Recursive Fibonacci");
|
2020-02-21 16:33:00 -05:00
|
|
|
for i in [0usize, 10, 20, 25, 26, 27, 28, 29, 30].iter() {
|
2020-02-21 13:15:05 -05:00
|
|
|
group.bench_with_input(BenchmarkId::new("Naive Recursive", i), i, |b, i| {
|
2020-02-21 16:33:00 -05:00
|
|
|
b.iter(|| rec_fibonacci(black_box(*i)))
|
2020-02-21 13:15:05 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
group.finish();
|
2020-02-20 17:13:46 -05:00
|
|
|
}
|
|
|
|
|
2020-02-21 16:33:00 -05:00
|
|
|
fn bench_gcd(c: &mut Criterion) {
|
|
|
|
let mut group = c.benchmark_group("GCD");
|
|
|
|
|
|
|
|
let max = fibonacci(186).unwrap();
|
|
|
|
let max_1 = fibonacci(185).unwrap();
|
|
|
|
|
|
|
|
let med = fibonacci(93).unwrap();
|
|
|
|
let med_1 = fibonacci(92).unwrap();
|
|
|
|
|
|
|
|
let small = fibonacci(15).unwrap();
|
|
|
|
let small_1 = fibonacci(14).unwrap();
|
|
|
|
|
|
|
|
for input in [(small_1, small), (med_1, med), (max_1, max)].iter() {
|
|
|
|
let param = format!("{},{}", input.0, input.1);
|
2020-02-24 13:49:09 -05:00
|
|
|
group.bench_with_input(BenchmarkId::new("Binary", ¶m), input, |bench, input| {
|
|
|
|
let (a, b) = input;
|
|
|
|
bench.iter(|| u128::gcd(black_box(*a), black_box(*b)))
|
2020-02-21 16:33:00 -05:00
|
|
|
});
|
2020-02-24 13:49:09 -05:00
|
|
|
group.bench_with_input(BenchmarkId::new("Euclid", ¶m), input, |bench, input| {
|
|
|
|
let (a, b) = input;
|
|
|
|
bench.iter(|| u128::e_gcd(black_box(*a), black_box(*b)))
|
2020-02-21 16:33:00 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
group.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, bench_factorial, bench_fibonacci, bench_gcd);
|
2020-02-20 17:13:46 -05:00
|
|
|
criterion_main!(benches);
|