|
| 1 | +use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 2 | + |
| 3 | +use jpeg_encoder::{ImageBuffer, RgbImage}; |
| 4 | +use std::time::Duration; |
| 5 | + |
| 6 | +fn create_bench_img() -> (Vec<u8>, u16, u16) { |
| 7 | + let width = 1001; |
| 8 | + let height = 500; |
| 9 | + |
| 10 | + let mut data = Vec::with_capacity(width * height * 3); |
| 11 | + |
| 12 | + for y in 0..height { |
| 13 | + for x in 0..width { |
| 14 | + if (x * y) % 13 == 0 { |
| 15 | + data.push(0); |
| 16 | + data.push(0); |
| 17 | + data.push(0); |
| 18 | + } else if (x * y) % 17 == 0 { |
| 19 | + data.push(255); |
| 20 | + data.push(255); |
| 21 | + data.push(255); |
| 22 | + } else if (x * y) % 19 == 0 { |
| 23 | + data.push(255); |
| 24 | + data.push(0); |
| 25 | + data.push(0); |
| 26 | + } else if (x * y) % 21 == 0 { |
| 27 | + data.push(0); |
| 28 | + data.push(0); |
| 29 | + data.push(255); |
| 30 | + } else if (x * y) % 23 == 0 { |
| 31 | + data.push(0); |
| 32 | + data.push(255); |
| 33 | + data.push(0); |
| 34 | + } else if (x * y) % 25 == 0 { |
| 35 | + data.push(96); |
| 36 | + data.push(255); |
| 37 | + data.push(96); |
| 38 | + } else if (x * y) % 27 == 0 { |
| 39 | + data.push(255); |
| 40 | + data.push(96); |
| 41 | + data.push(96); |
| 42 | + } else if (x * y) % 29 == 0 { |
| 43 | + data.push(96); |
| 44 | + data.push(96); |
| 45 | + data.push(255); |
| 46 | + } else { |
| 47 | + data.push((x % 256) as u8); |
| 48 | + data.push((x % 256) as u8); |
| 49 | + data.push(((x * y) % 256) as u8); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + (data, width as u16, height as u16) |
| 55 | +} |
| 56 | + |
| 57 | +fn criterion_benchmark(c: &mut Criterion) { |
| 58 | + let (data, width, height) = create_bench_img(); |
| 59 | + let res1 = Vec::with_capacity(usize::from(width)); |
| 60 | + let res2 = Vec::with_capacity(usize::from(width)); |
| 61 | + let res3 = Vec::with_capacity(usize::from(width)); |
| 62 | + let res4 = Vec::with_capacity(usize::from(width)); |
| 63 | + |
| 64 | + let mut res = [res1, res2, res3, res4]; |
| 65 | + |
| 66 | + let mut group = c.benchmark_group("ycbcr"); |
| 67 | + group.measurement_time(Duration::from_secs(45)); |
| 68 | + group.warm_up_time(Duration::from_secs(10)); |
| 69 | + |
| 70 | + group.bench_function("default ycbcr", |b| { |
| 71 | + let image_buffer = RgbImage(&data, width, height); |
| 72 | + |
| 73 | + b.iter(|| { |
| 74 | + for y in 0..height { |
| 75 | + res[0].clear(); |
| 76 | + res[1].clear(); |
| 77 | + res[2].clear(); |
| 78 | + image_buffer.fill_buffers(y, black_box(&mut res)); |
| 79 | + } |
| 80 | + black_box(&res); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))] |
| 85 | + group.bench_function("ycbcr avx2", |b| { |
| 86 | + use jpeg_encoder::RgbImageAVX2; |
| 87 | + let image_buffer = RgbImageAVX2(&data, width, height); |
| 88 | + |
| 89 | + b.iter(|| { |
| 90 | + for y in 0..height { |
| 91 | + res[0].clear(); |
| 92 | + res[1].clear(); |
| 93 | + res[2].clear(); |
| 94 | + image_buffer.fill_buffers(y, black_box(&mut res)); |
| 95 | + } |
| 96 | + black_box(&res); |
| 97 | + }) |
| 98 | + }); |
| 99 | + |
| 100 | + group.finish(); |
| 101 | +} |
| 102 | + |
| 103 | +criterion_group!(benches, criterion_benchmark); |
| 104 | +criterion_main!(benches); |
0 commit comments