summaryrefslogtreecommitdiff
path: root/benches/bench.rs
blob: cb3f4d9de6d923724b83d8aea7c1e45fc0f865ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use criterion::{criterion_group, criterion_main, Criterion};
use std::hint::black_box;

fn run_benches(c: &mut Criterion) {
	let large_image = black_box(include_bytes!("large.png"));
	let small_image = black_box(include_bytes!("small.png"));

	let mut group = c.benchmark_group("large-image");
	group.sample_size(50);

	group.bench_function("tiny-png", |b| {
		b.iter(|| {
			let png = &large_image[..];
			let header = tiny_png::decode_png_header(png).unwrap();
			let mut buf = vec![0; header.required_bytes()];
			let data = tiny_png::decode_png(png, &mut buf).unwrap();
			std::hint::black_box(data);
		})
	});

	group.bench_function("png", |b| {
		b.iter(|| {
			let png = &large_image[..];
			let decoder = png::Decoder::new(png);
			let mut reader = decoder.read_info().unwrap();
			let mut png_buf = vec![0; reader.output_buffer_size()];
			reader.next_frame(&mut png_buf).unwrap();
			std::hint::black_box(png_buf);
		})
	});
	group.finish();

	let mut group = c.benchmark_group("small-image");
	group.sample_size(1000);
	group.bench_function("tiny-png", |b| {
		b.iter(|| {
			let png = &small_image[..];
			let header = tiny_png::decode_png_header(png).unwrap();
			let mut buf = vec![0; header.required_bytes()];
			let data = tiny_png::decode_png(png, &mut buf).unwrap();
			std::hint::black_box(data);
		})
	});
	group.bench_function("png", |b| {
		b.iter(|| {
			let png = &small_image[..];
			let decoder = png::Decoder::new(png);
			let mut reader = decoder.read_info().unwrap();
			let mut png_buf = vec![0; reader.output_buffer_size()];
			reader.next_frame(&mut png_buf).unwrap();
			std::hint::black_box(png_buf);
		})
	});
	group.finish();
}

criterion_group!(benches, run_benches);
criterion_main!(benches);