Some minor stuff for bigints
timw4mail/rusty-numbers/pipeline/head There was a failure building this commit Details

This commit is contained in:
Timothy Warren 2020-02-24 16:30:59 -05:00
parent 074f2bf662
commit 57526e5fed
3 changed files with 45 additions and 16 deletions

View File

@ -42,24 +42,39 @@ fn bench_fibonacci(c: &mut Criterion) {
fn bench_gcd(c: &mut Criterion) {
let mut group = c.benchmark_group("GCD");
let max = fibonacci(186).unwrap();
let max_1 = fibonacci(185).unwrap();
#[derive(Debug)]
struct Gcd {
left: u128,
right: u128,
left_fib: u128,
right_fib: u128,
}
let med = fibonacci(93).unwrap();
let med_1 = fibonacci(92).unwrap();
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 small = fibonacci(15).unwrap();
let small_1 = fibonacci(14).unwrap();
let max = Gcd::new(185, 186);
let med = Gcd::new(92, 93);
let small = Gcd::new(14, 15);
for input in [(small_1, small), (med_1, med), (max_1, max)].iter() {
let param = format!("{},{}", input.0, input.1);
group.bench_with_input(BenchmarkId::new("Binary", &param), input, |bench, input| {
let (a, b) = input;
bench.iter(|| u128::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("Euclid", &param), input, |bench, input| {
let (a, b) = input;
bench.iter(|| u128::e_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();

View File

@ -58,6 +58,14 @@ impl BigInt {
Self::default()
}
pub fn shrink_to_fit(&mut self) {
todo!();
}
pub fn from_str_radix() {
todo!();
}
/// Split an unsigned number into BigInt parts
fn split<T: Unsigned>(&mut self, num: T) -> Vec<usize> {
// Pretty easy if you don't actually need to split the value!
@ -71,4 +79,11 @@ impl BigInt {
}
#[cfg(test)]
mod tests {}
mod tests {
use super::*;
#[test]
fn sanity_checks() {
}
}

View File

@ -32,7 +32,6 @@ pub fn mem_fibonacci(n: usize) -> Option<u128> {
table[2] = 1;
/// Actual calculating function for `fibonacci`
#[inline]
fn f(n: usize, table: &mut [u128]) -> Option<u128> {
if n < 2 {
// The first values are predefined.