Debian Codegraph @

.cargo/vendor/v_frame/benches/bench.rs

raw
1 use std::hint::black_box;
2
3 use criterion::{criterion_group, criterion_main, Criterion};
4 use v_frame::frame::Frame;
5 use v_frame::pixel::{CastFromPrimitive, ChromaSampling};
6 use v_frame::plane::Plane;
7
8 fn frame(c: &mut Criterion) {
9 c.bench_function("frame new_with_padding padding=0", |b| {
10 b.iter(|| {
11 Frame::<u8>::new_with_padding(
12 black_box(640),
13 black_box(480),
14 black_box(ChromaSampling::Cs420),
15 black_box(0),
16 )
17 })
18 });
19
20 c.bench_function("frame new_with_padding padding!=0", |b| {
21 b.iter(|| {
22 Frame::<u8>::new_with_padding(
23 black_box(640),
24 black_box(480),
25 black_box(ChromaSampling::Cs420),
26 black_box(24),
27 )
28 })
29 });
30 }
31
32 fn plane(c: &mut Criterion) {
33 c.bench_function("plane new padding=0", |b| {
34 b.iter(|| {
35 Plane::<u8>::new(
36 black_box(640),
37 black_box(480),
38 black_box(1),
39 black_box(1),
40 black_box(0),
41 black_box(0),
42 )
43 })
44 });
45
46 c.bench_function("plane new padding!=0", |b| {
47 b.iter(|| {
48 Plane::<u8>::new(
49 black_box(640),
50 black_box(480),
51 black_box(1),
52 black_box(1),
53 black_box(24),
54 black_box(24),
55 )
56 })
57 });
58
59 let p8b = Plane::<u8>::new(
60 black_box(640),
61 black_box(480),
62 black_box(1),
63 black_box(1),
64 black_box(0),
65 black_box(0),
66 );
67
68 // We need to use PerIteration because we modify the Plane every iteration
69 let batch_size = criterion::BatchSize::PerIteration;
70
71 c.bench_function("plane clone", |b| b.iter(|| p8b.clone()));
72
73 c.bench_function("plane pad", |b| {
74 b.iter_batched_ref(
75 || p8b.clone(),
76 |p| p.pad(black_box(680), black_box(520)),
77 batch_size,
78 )
79 });
80
81 let data_8b: Vec<u8> = vec![2; 640 * 480];
82 c.bench_function("plane copy_from_raw_u8 8-bit", |b| {
83 b.iter_batched_ref(
84 || p8b.clone(),
85 |p| p.copy_from_raw_u8(black_box(&data_8b), black_box(640), black_box(1)),
86 batch_size,
87 )
88 });
89
90 let p10b = Plane::<u16>::new(
91 black_box(640),
92 black_box(480),
93 black_box(1),
94 black_box(1),
95 black_box(0),
96 black_box(0),
97 );
98 let data_10b: Vec<u8> = vec![2; 640 * 480 * 2];
99 c.bench_function("plane copy_from_raw_u8 10-bit", |b| {
100 b.iter_batched_ref(
101 || p10b.clone(),
102 |p| p.copy_from_raw_u8(black_box(&data_10b), black_box(640), black_box(2)),
103 batch_size,
104 )
105 });
106
107 c.bench_function("plane downsampled", |b| {
108 b.iter(|| p8b.downsampled(black_box(320), black_box(240)))
109 });
110
111 c.bench_function("plane downscale", |b| b.iter(|| p8b.downscale::<2>()));
112
113 // This may seem silly to benchmark, but there is some math in the iterator
114 // that has been known to hinder compiler optimizations
115 c.bench_function("plane rows_iter", |b| {
116 b.iter(|| {
117 p8b.rows_iter()
118 .map(|r| r.iter().map(|v| u8::cast_from(*v) as u64).sum::<u64>())
119 .collect::<Vec<_>>()
120 })
121 });
122 }
123
124 criterion_group!(benches, frame, plane);
125 criterion_main!(benches);