Skip to content

Commit f59b547

Browse files
committed
Add benchmark for neuquant
There were no benchmarks for neuquant, and it's worthwhile to figure out what consumes time when quantizing the colors. This creates two benchmarks, one for the creation of the quantization, and one for the searching of the nearest color. It tests on a static image which is mostly green and red.
1 parent 6c302d2 commit f59b547

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

benches/neuquant.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#![feature(test)]
2+
3+
extern crate test;
4+
5+
use color_quant::NeuQuant;
6+
use test::{black_box, Bencher};
7+
8+
fn make_pixels() -> Vec<u8> {
9+
let mut pixels = vec![0; 4 * 256 * 256];
10+
let mut r: u8 = 0;
11+
let mut g: u8 = 0;
12+
let mut b: u8 = 0;
13+
14+
for (i, p) in pixels.iter_mut().enumerate() {
15+
match i % 4 {
16+
0 => *p = r,
17+
1 => *p = g,
18+
2 => {
19+
*p = b;
20+
if let Some(next_r) = r.checked_add(1) {
21+
r = next_r;
22+
continue;
23+
}
24+
r = 0;
25+
if let Some(next_g) = g.checked_add(1) {
26+
g = next_g;
27+
continue;
28+
}
29+
g = 0;
30+
b += 1
31+
}
32+
3 => *p = 255,
33+
_ => unreachable!(),
34+
}
35+
}
36+
pixels
37+
}
38+
39+
#[bench]
40+
fn bench_map_pixel(b: &mut Bencher) {
41+
let pixels = make_pixels();
42+
let nq = NeuQuant::new(10, 256, &pixels);
43+
let mut i = 0u8;
44+
b.iter(|| {
45+
i = i.wrapping_add(1);
46+
let mut color = black_box([123, i, 4, 255]);
47+
nq.map_pixel(&mut color[..]);
48+
})
49+
}
50+
51+
#[bench]
52+
fn bench_new_nq(b: &mut Bencher) {
53+
let pixels = make_pixels();
54+
b.iter(|| NeuQuant::new(10, 128, &pixels));
55+
}

0 commit comments

Comments
 (0)