Skip to content

Commit e209413

Browse files
committed
Merge branch 'master' into 4chan
-X theirs
2 parents 85c64cb + c133495 commit e209413

File tree

1 file changed

+116
-100
lines changed

1 file changed

+116
-100
lines changed

benches/conversions.rs

+116-100
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,136 @@
1+
use colcon::Space;
12
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2-
use colcon::{Space, convert_space};
33

4-
fn pixels() -> Box<[f32]> {
4+
fn pixels() -> Vec<f32> {
55
let size = 512;
66
let mut result = Vec::<f32>::with_capacity(size * size * 3);
77
for x in 1..=size {
88
for y in 1..=size {
99
let n = (x as f32 / size as f32 / 2.0) + (y as f32 / size as f32 / 2.0);
10-
result.extend_from_slice(&[n, n, n]);
10+
result.extend_from_slice(&[n; 3]);
1111
}
1212
}
13-
result.into_boxed_slice()
13+
result
1414
}
1515

16-
pub fn conversions(c: &mut Criterion) {
17-
let pixels = pixels();
18-
19-
c.bench_function("srgb_to_lrgb", |b| b.iter(|| {
20-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::srgb_to_lrgb::<_, 3>(pixel.try_into().unwrap())));
21-
} ));
22-
23-
c.bench_function("lrgb_to_xyz", |b| b.iter(|| {
24-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::lrgb_to_xyz(pixel.try_into().unwrap())));
25-
} ));
26-
27-
c.bench_function("xyz_to_cielab", |b| b.iter(|| {
28-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::xyz_to_cielab(pixel.try_into().unwrap())));
29-
} ));
30-
31-
c.bench_function("xyz_to_oklab", |b| b.iter(|| {
32-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::xyz_to_oklab(pixel.try_into().unwrap())));
33-
} ));
34-
35-
c.bench_function("xyz_to_jzazbz", |b| b.iter(|| {
36-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::xyz_to_jzazbz(pixel.try_into().unwrap())));
37-
} ));
38-
39-
c.bench_function("lab_to_lch", |b| b.iter(|| {
40-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::lab_to_lch(pixel.try_into().unwrap())));
41-
} ));
42-
43-
c.bench_function("lch_to_lab", |b| b.iter(|| {
44-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::lch_to_lab(pixel.try_into().unwrap())));
45-
} ));
46-
47-
c.bench_function("jzazbz_to_xyz", |b| b.iter(|| {
48-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::jzazbz_to_xyz(pixel.try_into().unwrap())));
49-
} ));
50-
51-
c.bench_function("oklab_to_xyz", |b| b.iter(|| {
52-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::oklab_to_xyz(pixel.try_into().unwrap())));
53-
} ));
54-
55-
c.bench_function("cielab_to_xyz", |b| b.iter(|| {
56-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::cielab_to_xyz(pixel.try_into().unwrap())));
57-
} ));
58-
59-
c.bench_function("xyz_to_lrgb", |b| b.iter(|| {
60-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::xyz_to_lrgb(pixel.try_into().unwrap())));
61-
} ));
62-
63-
c.bench_function("lrgb_to_srgb", |b| b.iter(|| {
64-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::lrgb_to_srgb(pixel.try_into().unwrap())));
65-
} ));
66-
67-
c.bench_function("srgb_to_hsv", |b| b.iter(|| {
68-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::srgb_to_hsv(pixel.try_into().unwrap())));
69-
} ));
70-
71-
c.bench_function("hsv_to_srgb", |b| b.iter(|| {
72-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::hsv_to_srgb(pixel.try_into().unwrap())));
73-
} ));
74-
75-
c.bench_function("srgb_eotf", |b| b.iter(|| {
76-
black_box(pixels.clone().iter_mut().for_each(|n| *n = colcon::srgb_eotf(*n)));
77-
} ));
78-
79-
c.bench_function("srgb_eotf_inverse", |b| b.iter(|| {
80-
black_box(pixels.clone().iter_mut().for_each(|n| *n = colcon::srgb_oetf(*n)));
81-
} ));
82-
83-
c.bench_function("full_to", |b| b.iter(|| {
84-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| convert_space(Space::SRGB, Space::CIELCH, pixel.try_into().unwrap())));
85-
} ));
86-
87-
c.bench_function("full_from", |b| b.iter(|| {
88-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| convert_space(Space::CIELCH, Space::SRGB, pixel.try_into().unwrap())));
89-
} ));
90-
91-
c.bench_function("full_to_chunk", |b| b.iter(|| {
92-
black_box(colcon::convert_space_chunked(Space::CIELCH, Space::SRGB, pixels.chunks_exact(3).map(|chunk| chunk.try_into().unwrap()).collect::<Vec<[f32; 3]>>().as_mut_slice()));
93-
} ));
16+
macro_rules! bench_three_generic {
17+
($c: expr, $pc: expr, $f: path, $id:literal, $n:literal, $t:ty, $ts:literal) => {
18+
$c.bench_function(concat!($id, "_", $n, $ts), |b| {
19+
b.iter(|| {
20+
let mut pixels = $pc.clone();
21+
black_box(pixels.iter_mut().for_each(|pixel| $f(pixel)));
22+
})
23+
})
24+
};
25+
}
9426

95-
c.bench_function("full_from_chunk", |b| b.iter(|| {
96-
black_box(colcon::convert_space_chunked(Space::CIELCH, Space::SRGB, &mut pixels.chunks_exact(3).map(|chunk| chunk.try_into().unwrap()).collect::<Vec<[f32; 3]>>().as_mut_slice()));
97-
} ));
27+
macro_rules! bench_one_generic {
28+
($c: expr, $ps: expr, $f: path, $id:literal, $t:ty, $ts:literal) => {
29+
$c.bench_function(concat!($id, "_", $ts), |b| {
30+
b.iter(|| {
31+
let mut pixels = $ps.clone();
32+
black_box(pixels.iter_mut().for_each(|n| *n = $f(*n)));
33+
})
34+
})
35+
};
36+
}
9837

99-
c.bench_function("full_to_slice", |b| b.iter(|| {
100-
black_box(colcon::convert_space_sliced(Space::CIELCH, Space::SRGB, &mut pixels.clone()));
101-
} ));
38+
macro_rules! bench_convert_generic {
39+
($c: expr, $ps: expr, $pc: expr, $from: expr, $to:expr, $id:literal, $n:literal, $t:ty, $ts:literal) => {
40+
$c.bench_function(concat!($id, "_", $n, $ts), |b| {
41+
b.iter(|| {
42+
let mut pixels = $pc.clone();
43+
black_box(
44+
pixels
45+
.iter_mut()
46+
.for_each(|pixel| colcon::convert_space($from, $to, pixel)),
47+
);
48+
})
49+
});
50+
51+
$c.bench_function(concat!($id, "_", $n, $ts, "_chunk"), |b| {
52+
b.iter(|| {
53+
let mut pixels = $pc.clone();
54+
black_box(colcon::convert_space_chunked($from, $to, &mut pixels));
55+
})
56+
});
57+
58+
$c.bench_function(concat!($id, "_", $n, $ts, "_slice"), |b| {
59+
b.iter(|| {
60+
let mut pixels = $ps.clone();
61+
black_box(colcon::convert_space_sliced($from, $to, &mut pixels));
62+
})
63+
});
64+
};
65+
}
10266

103-
c.bench_function("full_from_slice", |b| b.iter(|| {
104-
black_box(colcon::convert_space_sliced(Space::CIELCH, Space::SRGB, &mut pixels.clone()));
105-
} ));
67+
pub fn conversions(c: &mut Criterion) {
68+
let pix_slice_f32: Box<[f32]> = pixels().into_boxed_slice();
69+
70+
let pix_slice_f64: Box<[f64]> = pixels()
71+
.into_iter()
72+
.map(|c| c.into())
73+
.collect::<Vec<f64>>()
74+
.into_boxed_slice();
75+
76+
let pix_chunk_3f32: Box<[[f32; 3]]> = pixels()
77+
.chunks_exact(3)
78+
.map(|c| c.try_into().unwrap())
79+
.collect::<Vec<[f32; 3]>>()
80+
.into_boxed_slice();
81+
82+
let pix_chunk_3f64: Box<[[f64; 3]]> = pixels()
83+
.chunks_exact(3)
84+
.map(|c| TryInto::<[f32; 3]>::try_into(c).unwrap().map(|n| n.into()))
85+
.collect::<Vec<[f64; 3]>>()
86+
.into_boxed_slice();
87+
88+
macro_rules! bench_three {
89+
($f: path, $id:literal) => {
90+
bench_three_generic!(c, pix_chunk_3f32, $f, $id, 3, f32, "f32");
91+
bench_three_generic!(c, pix_chunk_3f64, $f, $id, 3, f64, "f64");
92+
};
93+
}
10694

107-
c.bench_function("single", |b| b.iter(|| {
108-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| convert_space(Space::LRGB, Space::XYZ, pixel.try_into().unwrap())));
109-
} ));
95+
macro_rules! bench_one {
96+
($f: path, $id:literal) => {
97+
bench_one_generic!(c, pix_slice_f32, $f, $id, f32, "f32");
98+
bench_one_generic!(c, pix_slice_f32, $f, $id, f64, "f64");
99+
};
100+
}
110101

111-
c.bench_function("single_chunk", |b| b.iter(|| {
112-
black_box(colcon::convert_space_chunked(Space::LRGB, Space::XYZ, pixels.chunks_exact(3).map(|chunk| chunk.try_into().unwrap()).collect::<Vec<[f32; 3]>>().as_mut_slice()));
113-
} ));
102+
macro_rules! bench_convert {
103+
($from: expr, $to:expr, $id:literal) => {
104+
bench_convert_generic!(c, pix_slice_f32, pix_chunk_3f32, $from, $to, $id, 3, f32, "f32");
105+
bench_convert_generic!(c, pix_slice_f64, pix_chunk_3f64, $from, $to, $id, 3, f64, "f64");
106+
};
107+
}
114108

115-
c.bench_function("single_slice", |b| b.iter(|| {
116-
black_box(colcon::convert_space_sliced(Space::LRGB, Space::XYZ, &mut pixels.clone()));
117-
} ));
109+
// Forward
110+
bench_three!(colcon::srgb_to_lrgb, "srgb_to_lrgb");
111+
bench_three!(colcon::lrgb_to_xyz, "lrgb_to_xyz");
112+
bench_three!(colcon::xyz_to_cielab, "xyz_to_cielab");
113+
bench_three!(colcon::xyz_to_oklab, "xyz_to_oklab");
114+
bench_three!(colcon::xyz_to_jzazbz, "xyz_to_jzazbz");
115+
bench_three!(colcon::lab_to_lch, "lab_to_lch");
116+
bench_three!(colcon::srgb_to_hsv, "srgb_to_hsv");
117+
// Backward
118+
bench_three!(colcon::lch_to_lab, "lch_to_lab");
119+
bench_three!(colcon::jzazbz_to_xyz, "jzazbz_to_xyz");
120+
bench_three!(colcon::oklab_to_xyz, "oklab_to_xyz");
121+
bench_three!(colcon::cielab_to_xyz, "cielab_to_xyz");
122+
bench_three!(colcon::xyz_to_lrgb, "xyz_to_lrgb");
123+
bench_three!(colcon::lrgb_to_srgb, "lrgb_to_srgb");
124+
bench_three!(colcon::hsv_to_srgb, "hsv_to_srgb");
125+
126+
bench_one!(colcon::srgb_eotf, "srgb_eotf");
127+
bench_one!(colcon::srgb_oetf, "srgb_oetf");
128+
bench_one!(colcon::pq_eotf, "pq_eotf");
129+
bench_one!(colcon::pq_oetf, "pq_oetf");
130+
131+
bench_convert!(Space::SRGB, Space::CIELCH, "full_forward");
132+
bench_convert!(Space::CIELCH, Space::SRGB, "full_backward");
133+
bench_convert!(Space::LRGB, Space::XYZ, "minimal");
118134
}
119135

120136
criterion_group!(benches, conversions);

0 commit comments

Comments
 (0)