Skip to content

Commit

Permalink
benches: Simplify
Browse files Browse the repository at this point in the history
Make use of the new Pixels, ColorConvert and Write facilities.

Signed-off-by: Christopher N. Hesse <raymanfx@gmail.com>
  • Loading branch information
raymanfx committed Oct 29, 2023
1 parent 356c742 commit a963d62
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
59 changes: 34 additions & 25 deletions ffimage-yuv/benches/convert.rs
Original file line number Diff line number Diff line change
@@ -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::<u8>([10, 10, 10]); res.0 * res.1];
let mut yuv = vec![Yuv::<u8>([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::<Yuv<u8>>()
.colorconvert::<Rgb<u8>>()
.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::<u8>([10, 10, 10]); res.0 * res.1];
let [mut yuv1, mut yuv2] = [Yuv::<u8>([0, 0, 0]); 2];
let mut yuyv = vec![Yuv422::<u8, 0, 2, 1, 3>([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::<Yuv422<u8, 0, 2, 1, 3>>()
.colorconvert::<[Yuv<u8>; 2]>()
.map(|yuv422| <[Yuv<u8>; 2]>::from(yuv422))
.flatten()
.map(|yuv| Rgb::<u8>::from(yuv))
.write(&mut rgb);
})
},
);
}
}

criterion_group!(benches, rgb_to_yuv, rgb_to_yuyv);
criterion_group!(benches, yuv_to_rgb, yuv422_to_rgb);
25 changes: 16 additions & 9 deletions ffimage/benches/convert.rs
Original file line number Diff line number Diff line change
@@ -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::<u8>([10, 10, 10]); res.0 * res.1];
let mut bgr = vec![Rgb::<u8, 2, 1, 0>([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::<Rgb<u8>>()
.colorconvert::<Bgr<u8>>()
.write(black_box(&mut bgr))
})
});
}
Expand All @@ -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::<u8>([10, 10, 10]); res.0 * res.1];
let mut gray = vec![Gray::<u8>([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::<Rgb<u8>>()
.colorconvert::<Gray<u8>>()
.write(black_box(&mut gray))
})
});
}
Expand Down

0 comments on commit a963d62

Please sign in to comment.