|
2 | 2 | extern crate criterion; |
3 | 3 |
|
4 | 4 | use criterion::Criterion; |
| 5 | +use rand::distributions::{Distribution, Uniform}; |
5 | 6 |
|
6 | 7 | fn criterion_benchmark(c: &mut Criterion) { |
| 8 | + // f32 |
7 | 9 | for &n in &[100, 1000, 10000] { |
8 | | - c.bench_function(&format!("cos{}", n), |b| { |
9 | | - let in_ = vec![0.0_f64; n]; |
10 | | - let mut out = vec![0.0_f64; n]; |
| 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| { |
11 | 49 | b.iter(|| { |
12 | 50 | for i in 0..n { |
13 | 51 | out[i] = in_[i].cos(); |
14 | 52 | } |
15 | 53 | }) |
16 | 54 | }); |
17 | 55 |
|
18 | | - c.bench_function(&format!("vcos{}", n), |b| { |
19 | | - let in_ = vec![0.0_f64; n]; |
20 | | - let mut out = vec![0.0_f64; n]; |
| 56 | + c.bench_function(&format!("vcos64_n{}", n), |b| { |
21 | 57 | b.iter(|| unsafe { |
22 | 58 | intel_mkl_sys::vdCos(n as i32, in_.as_ptr(), out.as_mut_ptr()); |
23 | 59 | }) |
|
0 commit comments