Skip to content

Instantly share code, notes, and snippets.

@ttsugriy
Created July 9, 2023 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttsugriy/2639a984ce4e693e1b44df1715cd4be7 to your computer and use it in GitHub Desktop.
Save ttsugriy/2639a984ce4e693e1b44df1715cd4be7 to your computer and use it in GitHub Desktop.
fadd_fast benchmark
#![feature(core_intrinsics)]
use std::intrinsics::fadd_fast;
use criterion::{criterion_group, criterion_main, Criterion};
const XS: &'static [f32] = &[3.14; 1000];
fn slow_sum(xs: &[f32]) -> f32 {
xs.iter().sum()
}
fn fast_sum(xs: &[f32]) -> f32 {
xs.iter().fold(0.0, |x, y| unsafe { fadd_fast(x, *y) })
}
fn bench_sums(c: &mut Criterion) {
let mut group = c.benchmark_group("f32 sum");
group.bench_function("slow sum", |b| b.iter(|| slow_sum(XS)));
group.bench_function("fast sum", |b| b.iter(|| fast_sum(XS)));
group.finish();
}
criterion_group!(benches, bench_sums);
criterion_main!(benches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment