From a963d62d7ed9791ebda8d18f3ecb514c9ca4e121 Mon Sep 17 00:00:00 2001 From: "Christopher N. Hesse" Date: Mon, 6 Feb 2023 16:43:52 +0100 Subject: [PATCH] benches: Simplify Make use of the new Pixels, ColorConvert and Write facilities. Signed-off-by: Christopher N. Hesse --- ffimage-yuv/benches/convert.rs | 59 ++++++++++++++++++++-------------- ffimage/benches/convert.rs | 25 ++++++++------ 2 files changed, 50 insertions(+), 34 deletions(-) diff --git a/ffimage-yuv/benches/convert.rs b/ffimage-yuv/benches/convert.rs index 8ec9b7f..cb1e51c 100644 --- a/ffimage-yuv/benches/convert.rs +++ b/ffimage-yuv/benches/convert.rs @@ -1,45 +1,54 @@ use criterion::{black_box, criterion_group, Criterion}; -use ffimage::color::Rgb; +use ffimage::{ + color::Rgb, + iter::{ColorConvertExt, PixelsExt, WriteExt}, +}; use ffimage_yuv::{yuv::Yuv, yuv422::Yuv422}; -pub fn rgb_to_yuv(c: &mut Criterion) { +pub fn yuv_to_rgb(c: &mut Criterion) { let resolutions = [(640, 480), (1280, 720)]; for res in resolutions { - let rgb = vec![Rgb::([10, 10, 10]); res.0 * res.1]; - let mut yuv = vec![Yuv::([0, 0, 0]); res.0 * res.1]; + let yuv = vec![10; res.0 * res.1 * 3]; + let mut rgb = vec![10; res.0 * res.1 * 3]; - c.bench_function(&format!("Rgb[u8] -> Yuv[u8] ({}x{})", res.0, res.1), |b| { + c.bench_function(&format!("Yuv[u8] -> Rgb[u8] ({}x{})", res.0, res.1), |b| { b.iter(|| { - rgb.iter() - .zip(yuv.iter_mut()) - .for_each(|(rgb, yuv)| black_box(*yuv = (*rgb).into())) + yuv.iter() + .copied() + .pixels::>() + .colorconvert::>() + .write(black_box(&mut rgb)) }) }); } } -pub fn rgb_to_yuyv(c: &mut Criterion) { +pub fn yuv422_to_rgb(c: &mut Criterion) { let resolutions = [(640, 480), (1280, 720)]; for res in resolutions { - let rgb = vec![Rgb::([10, 10, 10]); res.0 * res.1]; - let [mut yuv1, mut yuv2] = [Yuv::([0, 0, 0]); 2]; - let mut yuyv = vec![Yuv422::([0, 0, 0, 0]); (res.0 * res.1) / 2]; - - c.bench_function(&format!("Rgb[u8] -> Yuyv[u8] ({}x{})", res.0, res.1), |b| { - b.iter(|| { - (rgb.iter().zip(rgb.iter().skip(1))) - .zip(yuyv.iter_mut()) - .for_each(|((rgb1, rgb2), yuyv)| { - black_box(yuv1 = (*rgb1).into()); - black_box(yuv2 = (*rgb2).into()); - black_box(*yuyv = [yuv1, yuv2].into()); - }) - }) - }); + let yuv422 = vec![10; res.0 * res.1 * 2]; + let mut rgb = vec![10; res.0 * res.1 * 3]; + + c.bench_function( + &format!("Yuv422[u8] -> Rgb[u8] ({}x{})", res.0, res.1), + |b| { + b.iter(|| { + yuv422 + .iter() + .copied() + .pixels::>() + .colorconvert::<[Yuv; 2]>() + .map(|yuv422| <[Yuv; 2]>::from(yuv422)) + .flatten() + .map(|yuv| Rgb::::from(yuv)) + .write(&mut rgb); + }) + }, + ); } } -criterion_group!(benches, rgb_to_yuv, rgb_to_yuyv); +criterion_group!(benches, yuv_to_rgb, yuv422_to_rgb); diff --git a/ffimage/benches/convert.rs b/ffimage/benches/convert.rs index 495eacb..709ded3 100644 --- a/ffimage/benches/convert.rs +++ b/ffimage/benches/convert.rs @@ -1,19 +1,24 @@ use criterion::{black_box, criterion_group, Criterion}; -use ffimage::color::{Gray, Rgb}; +use ffimage::{ + color::{Bgr, Gray, Rgb}, + iter::{ColorConvertExt, PixelsExt, WriteExt}, +}; pub fn rgb_to_bgr(c: &mut Criterion) { let resolutions = [(640, 480), (1280, 720)]; for res in resolutions { - let rgb = vec![Rgb::([10, 10, 10]); res.0 * res.1]; - let mut bgr = vec![Rgb::([0, 0, 0]); res.0 * res.1]; + let rgb = vec![10; res.0 * res.1 * 3]; + let mut bgr = vec![10; res.0 * res.1 * 3]; c.bench_function(&format!("Rgb[u8] -> Bgr[u8] ({}x{})", res.0, res.1), |b| { b.iter(|| { rgb.iter() - .zip(bgr.iter_mut()) - .for_each(|(rgb, bgr)| black_box(*bgr = (*rgb).into())) + .copied() + .pixels::>() + .colorconvert::>() + .write(black_box(&mut bgr)) }) }); } @@ -23,14 +28,16 @@ pub fn rgb_to_gray(c: &mut Criterion) { let resolutions = [(640, 480), (1280, 720)]; for res in resolutions { - let rgb = vec![Rgb::([10, 10, 10]); res.0 * res.1]; - let mut gray = vec![Gray::([0]); res.0 * res.1]; + let rgb = vec![10; res.0 * res.1 * 3]; + let mut gray = vec![10; res.0 * res.1 * 1]; c.bench_function(&format!("Rgb[u8] -> Gray[u8] ({}x{})", res.0, res.1), |b| { b.iter(|| { rgb.iter() - .zip(gray.iter_mut()) - .for_each(|(rgb, gray)| black_box(*gray = (*rgb).into())) + .copied() + .pixels::>() + .colorconvert::>() + .write(black_box(&mut gray)) }) }); }