No code coverage for benchmarks
Some checks failed
timw4mail/rusty-numbers/pipeline/head There was a failure building this commit
Some checks failed
timw4mail/rusty-numbers/pipeline/head There was a failure building this commit
This commit is contained in:
parent
272b7360f5
commit
f352c1aa4e
@ -1,101 +1,107 @@
|
||||
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
|
||||
use rusty_numbers::num::Unsigned;
|
||||
use rusty_numbers::{factorial, fibonacci, it_factorial, mem_fibonacci, rec_fibonacci};
|
||||
use criterion::{criterion_group, criterion_main};
|
||||
|
||||
fn bench_factorial(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("Factorials");
|
||||
#[cfg_attr(tarpaulin, skip)]
|
||||
mod sf {
|
||||
use criterion::{black_box, BenchmarkId, Criterion};
|
||||
use rusty_numbers::num::Unsigned;
|
||||
use rusty_numbers::{factorial, fibonacci, it_factorial, mem_fibonacci, rec_fibonacci};
|
||||
|
||||
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(black_box(*i)))
|
||||
});
|
||||
group.bench_with_input(BenchmarkId::new("Iterative", i), i, |b, i| {
|
||||
b.iter(|| it_factorial(black_box(*i)))
|
||||
});
|
||||
}
|
||||
group.finish();
|
||||
}
|
||||
pub fn bench_factorial(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("Factorials");
|
||||
|
||||
fn bench_fibonacci(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("Fibonacci");
|
||||
|
||||
for i in [0usize, 10, 20, 30, 40, 50, 70, 93, 140, 186].iter() {
|
||||
group.bench_with_input(BenchmarkId::new("Recursive memoized", i), i, |b, i| {
|
||||
b.iter(|| mem_fibonacci(black_box(*i)))
|
||||
});
|
||||
group.bench_with_input(BenchmarkId::new("Iterative", i), i, |b, i| {
|
||||
b.iter(|| fibonacci(black_box(*i)))
|
||||
});
|
||||
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(black_box(*i)))
|
||||
});
|
||||
group.bench_with_input(BenchmarkId::new("Iterative", i), i, |b, i| {
|
||||
b.iter(|| it_factorial(black_box(*i)))
|
||||
});
|
||||
}
|
||||
group.finish();
|
||||
}
|
||||
|
||||
group.finish();
|
||||
pub fn bench_fibonacci(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("Fibonacci");
|
||||
|
||||
let mut group = c.benchmark_group("Recursive Fibonacci");
|
||||
for i in [0usize, 10, 20, 25, 26, 27, 28, 29, 30].iter() {
|
||||
group.bench_with_input(BenchmarkId::new("Naive Recursive", i), i, |b, i| {
|
||||
b.iter(|| rec_fibonacci(black_box(*i)))
|
||||
});
|
||||
}
|
||||
group.finish();
|
||||
}
|
||||
for i in [0usize, 10, 20, 30, 40, 50, 70, 93, 140, 186].iter() {
|
||||
group.bench_with_input(BenchmarkId::new("Recursive memoized", i), i, |b, i| {
|
||||
b.iter(|| mem_fibonacci(black_box(*i)))
|
||||
});
|
||||
group.bench_with_input(BenchmarkId::new("Iterative", i), i, |b, i| {
|
||||
b.iter(|| fibonacci(black_box(*i)))
|
||||
});
|
||||
}
|
||||
|
||||
fn bench_gcd(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("GCD");
|
||||
group.finish();
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Gcd {
|
||||
left: u128,
|
||||
right: u128,
|
||||
left_fib: u128,
|
||||
right_fib: u128,
|
||||
let mut group = c.benchmark_group("Recursive Fibonacci");
|
||||
for i in [0usize, 10, 20, 25, 26, 27, 28, 29, 30].iter() {
|
||||
group.bench_with_input(BenchmarkId::new("Naive Recursive", i), i, |b, i| {
|
||||
b.iter(|| rec_fibonacci(black_box(*i)))
|
||||
});
|
||||
}
|
||||
group.finish();
|
||||
}
|
||||
|
||||
impl Gcd {
|
||||
fn new(left: u128, right: u128) -> Self {
|
||||
Gcd {
|
||||
left,
|
||||
right,
|
||||
left_fib: fibonacci(left as usize).unwrap(),
|
||||
right_fib: fibonacci(right as usize).unwrap(),
|
||||
pub fn bench_gcd(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("GCD");
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Gcd {
|
||||
left: u128,
|
||||
right: u128,
|
||||
left_fib: u128,
|
||||
right_fib: u128,
|
||||
}
|
||||
|
||||
impl Gcd {
|
||||
fn new(left: u128, right: u128) -> Self {
|
||||
Gcd {
|
||||
left,
|
||||
right,
|
||||
left_fib: fibonacci(left as usize).unwrap(),
|
||||
right_fib: fibonacci(right as usize).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let max = Gcd::new(185, 186);
|
||||
let med = Gcd::new(92, 93);
|
||||
let small = Gcd::new(14, 15);
|
||||
let max = Gcd::new(185, 186);
|
||||
let med = Gcd::new(92, 93);
|
||||
let small = Gcd::new(14, 15);
|
||||
|
||||
for input in [small, med, max].iter() {
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("Binary", input.left),
|
||||
input,
|
||||
|bench, input| {
|
||||
let a = input.left_fib;
|
||||
let b = input.right_fib;
|
||||
bench.iter(|| u128::gcd(black_box(a), black_box(b)))
|
||||
},
|
||||
);
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("Stein", input.left),
|
||||
input,
|
||||
|bench, input| {
|
||||
let a = input.left_fib;
|
||||
let b = input.right_fib;
|
||||
bench.iter(|| u128::stein_gcd(black_box(a), black_box(b)))
|
||||
},
|
||||
);
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("Euclid", input.left),
|
||||
input,
|
||||
|bench, input| {
|
||||
let a = input.left_fib;
|
||||
let b = input.right_fib;
|
||||
bench.iter(|| u128::e_gcd(black_box(a), black_box(b)))
|
||||
},
|
||||
);
|
||||
for input in [small, med, max].iter() {
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("Binary", input.left),
|
||||
input,
|
||||
|bench, input| {
|
||||
let a = input.left_fib;
|
||||
let b = input.right_fib;
|
||||
bench.iter(|| u128::gcd(black_box(a), black_box(b)))
|
||||
},
|
||||
);
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("Stein", input.left),
|
||||
input,
|
||||
|bench, input| {
|
||||
let a = input.left_fib;
|
||||
let b = input.right_fib;
|
||||
bench.iter(|| u128::stein_gcd(black_box(a), black_box(b)))
|
||||
},
|
||||
);
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("Euclid", input.left),
|
||||
input,
|
||||
|bench, input| {
|
||||
let a = input.left_fib;
|
||||
let b = input.right_fib;
|
||||
bench.iter(|| u128::e_gcd(black_box(a), black_box(b)))
|
||||
},
|
||||
);
|
||||
}
|
||||
group.finish();
|
||||
}
|
||||
group.finish();
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_factorial, bench_fibonacci, bench_gcd);
|
||||
criterion_group!(benches, sf::bench_factorial, sf::bench_fibonacci, sf::bench_gcd);
|
||||
criterion_main!(benches);
|
||||
|
||||
|
@ -6,7 +6,6 @@
|
||||
use std::f64::consts::PI;
|
||||
use std::f64::consts::E;
|
||||
|
||||
#[cfg_attr(tarpaulin, skip)]
|
||||
pub mod bigint;
|
||||
pub mod num;
|
||||
pub mod rational;
|
||||
|
Loading…
Reference in New Issue
Block a user