mirror of
https://github.com/Cian-H/fastmath.git
synced 2026-05-07 00:11:49 +01:00
Dev checkpoint - testing lookup tables
This commit is contained in:
+17
-19
@@ -5,18 +5,23 @@ use fastmath::*;
|
||||
use criterion::{Criterion, BenchmarkGroup, measurement::WallTime};
|
||||
use criterion::{black_box, criterion_group, criterion_main};
|
||||
|
||||
pub mod exact {
|
||||
include!("../src/tests/accuracy/exact.rs");
|
||||
}
|
||||
include!("../src/tests/accuracy/x.rs");
|
||||
|
||||
fn pow2_benchmarks(group: &mut BenchmarkGroup<WallTime>, x_f64: &[f64], x_f32: &[f32]) {
|
||||
group.bench_function("f64_fast", |b| {
|
||||
b.iter(|| x_f64.iter().map(|&x| black_box(x).fast_pow2()).collect::<Vec<f64>>())
|
||||
});
|
||||
group.bench_function("f64_builtin_fn", |b| {
|
||||
b.iter(|| x_f64.iter().map(|&x| 2.0f64.powf(black_box(x))).collect::<Vec<f64>>())
|
||||
b.iter(|| x_f64.iter().map(|&x| exact::f64::pow2(black_box(x))).collect::<Vec<f64>>())
|
||||
});
|
||||
group.bench_function("f32_fast", |b| {
|
||||
b.iter(|| x_f32.iter().map(|&x| black_box(x).fast_pow2()).collect::<Vec<f32>>())
|
||||
});
|
||||
group.bench_function("f32_builtin_fn", |b| {
|
||||
b.iter(|| x_f32.iter().map(|&x| 2.0f32.powf(black_box(x))).collect::<Vec<f32>>())
|
||||
b.iter(|| x_f32.iter().map(|&x| exact::f32::pow2(black_box(x))).collect::<Vec<f32>>())
|
||||
});
|
||||
}
|
||||
|
||||
@@ -25,13 +30,13 @@ fn exp_benchmarks(group: &mut BenchmarkGroup<WallTime>, x_f64: &[f64], x_f32: &[
|
||||
b.iter(|| x_f64.iter().map(|&x| black_box(x).fast_exp()).collect::<Vec<f64>>())
|
||||
});
|
||||
group.bench_function("f64_builtin", |b| {
|
||||
b.iter(|| x_f64.iter().map(|&x| black_box(x).exp()).collect::<Vec<f64>>())
|
||||
b.iter(|| x_f64.iter().map(|&x| exact::f64::exp(black_box(x))).collect::<Vec<f64>>())
|
||||
});
|
||||
group.bench_function("f32_fast", |b| {
|
||||
b.iter(|| x_f32.iter().map(|&x| black_box(x).fast_exp()).collect::<Vec<f32>>())
|
||||
});
|
||||
group.bench_function("f32_builtin", |b| {
|
||||
b.iter(|| x_f32.iter().map(|&x| black_box(x).exp()).collect::<Vec<f32>>())
|
||||
b.iter(|| x_f32.iter().map(|&x| exact::f32::exp(black_box(x))).collect::<Vec<f32>>())
|
||||
});
|
||||
}
|
||||
|
||||
@@ -43,7 +48,7 @@ fn cos_benchmarks(group: &mut BenchmarkGroup<WallTime>, x_f64: &[f64], x_f32: &[
|
||||
b.iter(|| x_f64.iter().map(|&x| black_box(x).lookup_cos()).collect::<Vec<f64>>())
|
||||
});
|
||||
group.bench_function("f64_builtin", |b| {
|
||||
b.iter(|| x_f64.iter().map(|&x| black_box(x).cos()).collect::<Vec<f64>>())
|
||||
b.iter(|| x_f64.iter().map(|&x| exact::f64::cos(black_box(x))).collect::<Vec<f64>>())
|
||||
});
|
||||
group.bench_function("f32_fast", |b| {
|
||||
b.iter(|| x_f32.iter().map(|&x| black_box(x).fast_cos()).collect::<Vec<f32>>())
|
||||
@@ -52,7 +57,7 @@ fn cos_benchmarks(group: &mut BenchmarkGroup<WallTime>, x_f64: &[f64], x_f32: &[
|
||||
b.iter(|| x_f32.iter().map(|&x| black_box(x).lookup_cos()).collect::<Vec<f32>>())
|
||||
});
|
||||
group.bench_function("f32_builtin", |b| {
|
||||
b.iter(|| x_f32.iter().map(|&x| black_box(x).cos()).collect::<Vec<f32>>())
|
||||
b.iter(|| x_f32.iter().map(|&x| exact::f32::cos(black_box(x))).collect::<Vec<f32>>())
|
||||
});
|
||||
}
|
||||
|
||||
@@ -61,42 +66,35 @@ fn sigmoid_benchmarks(group: &mut BenchmarkGroup<WallTime>, x_f64: &[f64], x_f32
|
||||
b.iter(|| x_f64.iter().map(|&x| black_box(x).fast_sigmoid()).collect::<Vec<f64>>())
|
||||
});
|
||||
group.bench_function("f64_builtin", |b| {
|
||||
b.iter(|| x_f64.iter().map(|&x| sigmoid_builtin_f64(black_box(x))).collect::<Vec<f64>>())
|
||||
b.iter(|| x_f64.iter().map(|&x| exact::f64::sigmoid(black_box(x))).collect::<Vec<f64>>())
|
||||
});
|
||||
group.bench_function("f32_fast", |b| {
|
||||
b.iter(|| x_f32.iter().map(|&x| black_box(x).fast_sigmoid()).collect::<Vec<f32>>())
|
||||
});
|
||||
group.bench_function("f32_builtin", |b| {
|
||||
b.iter(|| x_f32.iter().map(|&x| sigmoid_builtin_f32(black_box(x))).collect::<Vec<f32>>())
|
||||
b.iter(|| x_f32.iter().map(|&x| exact::f32::sigmoid(black_box(x))).collect::<Vec<f32>>())
|
||||
});
|
||||
}
|
||||
|
||||
fn criterion_benchmark(c: &mut Criterion) {
|
||||
// Prepare x values for testing functions
|
||||
let x_f64 = (-10000..10000)
|
||||
.map(|a| (a as f64) / 1000.)
|
||||
.collect::<Vec<f64>>();
|
||||
let x_f32 = (-10000..10000)
|
||||
.map(|a| (a as f32) / 1000.)
|
||||
.collect::<Vec<f32>>();
|
||||
// to ensure tests are fair, we need to instantiate the lookup tables
|
||||
1.0f64.lookup_cos();
|
||||
1.0f32.lookup_cos();
|
||||
// Then, tests can begin
|
||||
let mut group = c.benchmark_group("pow2");
|
||||
pow2_benchmarks(&mut group, &x_f64, &x_f32);
|
||||
pow2_benchmarks(&mut group, &X_F64, &X_F32);
|
||||
group.finish();
|
||||
|
||||
let mut group = c.benchmark_group("exp");
|
||||
exp_benchmarks(&mut group, &x_f64, &x_f32);
|
||||
exp_benchmarks(&mut group, &X_F64, &X_F32);
|
||||
group.finish();
|
||||
|
||||
let mut group = c.benchmark_group("cos");
|
||||
cos_benchmarks(&mut group, &x_f64, &x_f32);
|
||||
cos_benchmarks(&mut group, &X_F64, &X_F32);
|
||||
group.finish();
|
||||
|
||||
let mut group = c.benchmark_group("sigmoid");
|
||||
sigmoid_benchmarks(&mut group, &x_f64, &x_f32);
|
||||
sigmoid_benchmarks(&mut group, &X_F64, &X_F32);
|
||||
group.finish();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#![allow(dead_code, unused_imports)]
|
||||
|
||||
extern crate fastmath;
|
||||
|
||||
use fastmath::*;
|
||||
use criterion::{Criterion, BenchmarkGroup, measurement::WallTime};
|
||||
use criterion::{black_box, criterion_group, criterion_main};
|
||||
use std::f32::consts as f32_consts;
|
||||
use std::f64::consts as f64_consts;
|
||||
|
||||
pub mod exact {
|
||||
include!("../src/tests/accuracy/exact.rs");
|
||||
}
|
||||
include!("../src/tests/accuracy/x.rs");
|
||||
|
||||
fn dev_cos(x: f64) -> f64 {
|
||||
const ONE: f64 = 1.0;
|
||||
let v = ((((x + f64_consts::PI).abs()) % f64_consts::TAU) - f64_consts::PI).abs();
|
||||
let qpprox = ONE - f64_consts::FRAC_2_PI * v;
|
||||
qpprox + f64_consts::FRAC_PI_6 * qpprox * (ONE - qpprox * qpprox)
|
||||
}
|
||||
|
||||
fn devbench(group: &mut BenchmarkGroup<WallTime>) {
|
||||
group.bench_function("dev_cos", |b| {
|
||||
b.iter(|| X_F64.iter().map(|&x| dev_cos(black_box(x))).collect::<Vec<f64>>())
|
||||
});
|
||||
group.bench_function("fast_cos", |b| {
|
||||
b.iter(|| X_F64.iter().map(|&x| black_box(x).fast_cos()).collect::<Vec<f64>>())
|
||||
});
|
||||
group.bench_function("lookup_cos", |b| {
|
||||
b.iter(|| X_F64.iter().map(|&x| black_box(x).lookup_cos()).collect::<Vec<f64>>())
|
||||
});
|
||||
group.bench_function("builtin_cos", |b| {
|
||||
b.iter(|| X_F64.iter().map(|&x| exact::f64::cos(black_box(x))).collect::<Vec<f64>>())
|
||||
});
|
||||
}
|
||||
|
||||
fn criterion_benchmark(c: &mut Criterion) {
|
||||
// Then, tests can begin
|
||||
let mut group = c.benchmark_group("devbench");
|
||||
devbench(&mut group);
|
||||
group.finish();
|
||||
}
|
||||
|
||||
criterion_group!(devbenches, criterion_benchmark);
|
||||
criterion_main!(devbenches);
|
||||
Reference in New Issue
Block a user