Created
July 9, 2023 01:49
-
-
Save ttsugriy/2639a984ce4e693e1b44df1715cd4be7 to your computer and use it in GitHub Desktop.
fadd_fast benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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