use num_integer::Integer; use num_traits::{AsPrimitive, PrimInt, WrappingAdd, WrappingMul}; use std::cmp::{max, min}; use std::fmt::Debug; use test::{black_box, Bencher};
// --- Utilities for RNG ----------------------------------------------------
impl<T> BenchInteger for T where T: Integer + PrimInt + WrappingAdd + WrappingMul + 'static {}
// Simple PRNG so we don't have to worry about rand compatibility fn lcg<T>(x: T) -> T where
u32: AsPrimitive<T>,
T: BenchInteger,
{ // LCG parameters from Numerical Recipes // (but we're applying it to arbitrary sizes) const LCG_A: u32 = 1664525; const LCG_C: u32 = 1013904223;
x.wrapping_mul(&LCG_A.as_()).wrapping_add(&LCG_C.as_())
}
fn bench_unchecked<T, F>(b: &mut Bencher, v: &[(T, T)], f: F) where
T: Integer + Debug + Copy,
F: Fn(&T, &T) -> T,
{
b.iter(|| { for (x, y) in v {
black_box(f(x, y));
}
});
}
fn bench_ceil<T, F>(b: &mut Bencher, v: &[(T, T)], f: F) where
T: Integer + Debug + Copy,
F: Fn(&T, &T) -> T,
{ for &(i, j) in v { let rt = f(&i, &j); let (a, b) = (min(i, j), max(i, j)); // if both number are the same sign, check rt is in the middle if (a < T::zero()) == (b < T::zero()) { if (b - a).is_even() {
assert_eq!(rt - a, b - rt);
} else {
assert_eq!(rt - a, b - rt + T::one());
} // if both number have a different sign,
} else { if (a + b).is_even() {
assert_eq!(rt, (a + b) / (T::one() + T::one()))
} else {
assert_eq!(rt, (a + b + T::one()) / (T::one() + T::one()))
}
}
}
bench_unchecked(b, v, f);
}
fn bench_floor<T, F>(b: &mut Bencher, v: &[(T, T)], f: F) where
T: Integer + Debug + Copy,
F: Fn(&T, &T) -> T,
{ for &(i, j) in v { let rt = f(&i, &j); let (a, b) = (min(i, j), max(i, j)); // if both number are the same sign, check rt is in the middle if (a < T::zero()) == (b < T::zero()) { if (b - a).is_even() {
assert_eq!(rt - a, b - rt);
} else {
assert_eq!(rt - a + T::one(), b - rt);
} // if both number have a different sign,
} else { if (a + b).is_even() {
assert_eq!(rt, (a + b) / (T::one() + T::one()))
} else {
assert_eq!(rt, (a + b - T::one()) / (T::one() + T::one()))
}
}
}
bench_unchecked(b, v, f);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.