#![feature(portable_simd)] #![feature(is_sorted)] #![feature(sort_floats)] use criterion::{black_box, criterion_group, criterion_main, Criterion}; use simd_itertools::FindSimd; use simd_itertools::SIMD_LEN; use std::fmt::Debug; use std::simd::prelude::SimdPartialEq; use std::simd::Mask; use std::simd::{Simd, SimdElement}; fn benchmark_contains<'a, T: 'static + Copy + PartialEq + Default + Debug>( c: &mut Criterion, name: &str, ) where T: SimdElement + std::cmp::PartialEq + TryFrom + Debug, Simd: SimdPartialEq>, >::Error: Debug, { let len = 1000; let v1 = vec![T::default(); len]; let v2 = vec![T::default(); len]; let needle: T = 55.try_into().unwrap(); assert_eq!(v1, v2); c.bench_function(&format!("SIMD find {}", name), |b| { b.iter(|| black_box(v1.iter().find_simd(needle))) }); c.bench_function(&format!("trivial find {}", name), |b| { b.iter(|| black_box(v1.iter().find(|x| **x == needle))) }); } fn benchmark_contains_floats<'a, T: 'static + Copy + PartialEq + Default + Debug>( c: &mut Criterion, name: &str, ) where T: SimdElement + std::cmp::PartialEq + TryFrom + Debug + std::convert::From, Simd: SimdPartialEq>, >::Error: Debug, { let len = 1000; let v1 = vec![T::default(); len]; let v2 = vec![T::default(); len]; let needle: T = 55.0.try_into().unwrap(); assert_eq!(v1, v2); c.bench_function(&format!("SIMD all equal {}", name), |b| { b.iter(|| black_box(v1.iter().find_simd(needle))) }); c.bench_function(&format!("trivial all equal {}", name), |b| { b.iter(|| black_box(v1.iter().find(|x| **x == needle))) }); } fn criterion_benchmark(c: &mut Criterion) { benchmark_contains::(c, "u8"); benchmark_contains::(c, "i8"); benchmark_contains::(c, "u16"); benchmark_contains::(c, "i16"); benchmark_contains::(c, "u32"); benchmark_contains::(c, "i32"); benchmark_contains::(c, "u64"); benchmark_contains::(c, "i64"); benchmark_contains::(c, "isize"); benchmark_contains::(c, "usize"); benchmark_contains_floats::(c, "f32"); benchmark_contains_floats::(c, "f64"); } criterion_group!(benches, criterion_benchmark); criterion_main!(benches);