Skip to content

Commit 902c8db

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 902c8db

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

benches/neuquant.rs

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

0 commit comments

Comments
 (0)