|
| 1 | +#[macro_use] |
| 2 | +extern crate criterion; |
| 3 | + |
| 4 | +use criterion::Criterion; |
| 5 | +use rand::distributions::{Distribution, Uniform}; |
| 6 | + |
| 7 | +fn criterion_benchmark(c: &mut Criterion) { |
| 8 | + // f32 |
| 9 | + for &n in &[100, 1000, 10000] { |
| 10 | + let in_ = { |
| 11 | + let mut rng = rand::thread_rng(); |
| 12 | + let between = Uniform::from(0.0..2.0 * std::f32::consts::PI); |
| 13 | + let mut buf = vec![0.0; n]; |
| 14 | + for val in buf.iter_mut() { |
| 15 | + *val = between.sample(&mut rng); |
| 16 | + } |
| 17 | + buf |
| 18 | + }; |
| 19 | + |
| 20 | + let mut out = vec![0.0_f32; n]; |
| 21 | + c.bench_function(&format!("cos32_n{}", n), |b| { |
| 22 | + b.iter(|| { |
| 23 | + for i in 0..n { |
| 24 | + out[i] = in_[i].cos(); |
| 25 | + } |
| 26 | + }) |
| 27 | + }); |
| 28 | + c.bench_function(&format!("vcos32_n{}", n), |b| { |
| 29 | + b.iter(|| unsafe { |
| 30 | + intel_mkl_sys::vsCos(n as i32, in_.as_ptr(), out.as_mut_ptr()); |
| 31 | + }) |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + // f64 |
| 36 | + for &n in &[100, 1000, 10000] { |
| 37 | + let in_ = { |
| 38 | + let mut rng = rand::thread_rng(); |
| 39 | + let between = Uniform::from(0.0..2.0 * std::f64::consts::PI); |
| 40 | + let mut buf = vec![0.0; n]; |
| 41 | + for val in buf.iter_mut() { |
| 42 | + *val = between.sample(&mut rng); |
| 43 | + } |
| 44 | + buf |
| 45 | + }; |
| 46 | + |
| 47 | + let mut out = vec![0.0_f64; n]; |
| 48 | + c.bench_function(&format!("cos64_n{}", n), |b| { |
| 49 | + b.iter(|| { |
| 50 | + for i in 0..n { |
| 51 | + out[i] = in_[i].cos(); |
| 52 | + } |
| 53 | + }) |
| 54 | + }); |
| 55 | + |
| 56 | + c.bench_function(&format!("vcos64_n{}", n), |b| { |
| 57 | + b.iter(|| unsafe { |
| 58 | + intel_mkl_sys::vdCos(n as i32, in_.as_ptr(), out.as_mut_ptr()); |
| 59 | + }) |
| 60 | + }); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +criterion_group!(benches, criterion_benchmark); |
| 65 | +criterion_main!(benches); |
0 commit comments