Adds a few more benchmarks for poly operators. Changed benchmarks to use a seeded rng for reproducibility.

This commit is contained in:
Fletcher Haynes 2018-09-13 11:53:14 -07:00 committed by Andreas Fackler
parent 5f09f96345
commit e0c488a214
1 changed files with 45 additions and 8 deletions

View File

@ -8,13 +8,16 @@ use criterion::Criterion;
use pairing::bls12_381::Fr; use pairing::bls12_381::Fr;
use threshold_crypto::poly::Poly; use threshold_crypto::poly::Poly;
const TEST_DEGREES: [usize; 4] = [5, 10, 20, 40];
const RNG_SEED: [u32; 4] = [1, 2, 3, 4];
mod poly_benches { mod poly_benches {
use super::*; use super::*;
use rand::Rng; use rand::{Rng, SeedableRng, XorShiftRng};
// Benchmarks multiplication of two polynomials. /// Benchmarks multiplication of two polynomials.
fn multiplication(c: &mut Criterion) { fn multiplication(c: &mut Criterion) {
let mut rng = rand::thread_rng(); let mut rng = XorShiftRng::from_seed(RNG_SEED);
c.bench_function_over_inputs( c.bench_function_over_inputs(
"Polynomial multiplication", "Polynomial multiplication",
move |b, &&deg| { move |b, &&deg| {
@ -25,27 +28,61 @@ mod poly_benches {
}; };
b.iter_with_setup(rand_factors, |(lhs, rhs)| &lhs * &rhs) b.iter_with_setup(rand_factors, |(lhs, rhs)| &lhs * &rhs)
}, },
&[5, 10, 20, 40], &TEST_DEGREES,
); );
} }
// Benchmarks Lagrange interpolation for a polynomial. /// Benchmarks subtraction of two polynomials
fn subtraction(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
c.bench_function_over_inputs(
"Polynomial subtraction",
move |b, &&deg| {
let rand_factors = || {
let lhs = Poly::random(deg, &mut rng);
let rhs = Poly::random(deg, &mut rng);
(lhs, rhs)
};
b.iter_with_setup(rand_factors, |(lhs, rhs)| &lhs - &rhs)
},
&TEST_DEGREES,
);
}
/// Benchmarks addition of two polynomials
fn addition(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
c.bench_function_over_inputs(
"Polynomial addition",
move |b, &&deg| {
let rand_factors = || {
let lhs = Poly::random(deg, &mut rng);
let rhs = Poly::random(deg, &mut rng);
(lhs, rhs)
};
b.iter_with_setup(rand_factors, |(lhs, rhs)| &lhs + &rhs)
},
&TEST_DEGREES,
);
}
/// Benchmarks Lagrange interpolation for a polynomial.
fn interpolate(c: &mut Criterion) { fn interpolate(c: &mut Criterion) {
let mut rng = rand::thread_rng(); let mut rng = XorShiftRng::from_seed(RNG_SEED);
c.bench_function_over_inputs( c.bench_function_over_inputs(
"Polynomial interpolation", "Polynomial interpolation",
move |b, &&deg| { move |b, &&deg| {
let rand_samples = || (0..=deg).map(|i| (i, rng.gen::<Fr>())).collect::<Vec<_>>(); let rand_samples = || (0..=deg).map(|i| (i, rng.gen::<Fr>())).collect::<Vec<_>>();
b.iter_with_setup(rand_samples, Poly::interpolate) b.iter_with_setup(rand_samples, Poly::interpolate)
}, },
&[5, 10, 20, 40], &TEST_DEGREES,
); );
} }
criterion_group!{ criterion_group!{
name = poly_benches; name = poly_benches;
config = Criterion::default(); config = Criterion::default();
targets = multiplication, interpolate, targets = multiplication, interpolate, addition, subtraction,
} }
} }