From 902c8db4a8d9000a351e5ec27a96bd0dd6c14f2a Mon Sep 17 00:00:00 2001 From: julianknodt Date: Tue, 27 Jul 2021 12:38:09 -0700 Subject: [PATCH] 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. --- benches/neuquant.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 benches/neuquant.rs diff --git a/benches/neuquant.rs b/benches/neuquant.rs new file mode 100644 index 0000000..34d643e --- /dev/null +++ b/benches/neuquant.rs @@ -0,0 +1,56 @@ +#![feature(test)] + +extern crate color_quant; +extern crate test; + +use color_quant::NeuQuant; +use test::{black_box, Bencher}; + +fn make_pixels() -> Vec { + let mut pixels = vec![0; 4 * 256 * 256]; + let mut r: u8 = 0; + let mut g: u8 = 0; + let mut b: u8 = 0; + + for (i, p) in pixels.iter_mut().enumerate() { + match i % 4 { + 0 => *p = r, + 1 => *p = g, + 2 => { + *p = b; + if let Some(next_r) = r.checked_add(1) { + r = next_r; + continue; + } + r = 0; + if let Some(next_g) = g.checked_add(1) { + g = next_g; + continue; + } + g = 0; + b += 1 + } + 3 => *p = 255, + _ => unreachable!(), + } + } + pixels +} + +#[bench] +fn bench_map_pixel(b: &mut Bencher) { + let pixels = make_pixels(); + let nq = NeuQuant::new(10, 256, &pixels); + let mut i = 0u8; + b.iter(|| { + i = i.wrapping_add(1); + let mut color = black_box([123, i, 4, 255]); + nq.map_pixel(&mut color[..]); + }) +} + +#[bench] +fn bench_new_nq(b: &mut Bencher) { + let pixels = make_pixels(); + b.iter(|| NeuQuant::new(10, 128, &pixels)); +}