Skip to content

Commit be82a85

Browse files
committed
Fix benchmarks
1 parent 4618281 commit be82a85

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ edition = "2018"
1616

1717
[dependencies]
1818
crc-catalog = "2.1.0"
19+
20+
[dev-dependencies]
21+
criterion = { version = "0.3", features = ["html_reports"] }
22+
23+
[[bench]]
24+
name = "bench"
25+
harness = false

benches/bench.rs

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crc::*;
22
use criterion::{criterion_group, criterion_main};
3-
use criterion::{Benchmark, Criterion, Throughput};
3+
use criterion::{Criterion, Throughput};
44

55
pub const BLUETOOTH: Crc<u8> = Crc::<u8>::new(&CRC_8_BLUETOOTH);
66
pub const X25: Crc<u16> = Crc::<u16>::new(&CRC_16_IBM_SDLC);
@@ -9,64 +9,60 @@ pub const GSM_40: Crc<u64> = Crc::<u64>::new(&CRC_40_GSM);
99
pub const ECMA: Crc<u64> = Crc::<u64>::new(&CRC_64_ECMA_182);
1010
pub const DARC: Crc<u128> = Crc::<u128>::new(&CRC_82_DARC);
1111

12+
const INPUT_SIZE: usize = 1_000_000;
13+
1214
fn crc8(c: &mut Criterion) {
1315
let mut digest = BLUETOOTH.digest();
14-
let bytes = vec![0u8; 1_000_000];
15-
c.bench(
16-
"crc8",
17-
Benchmark::new("crc8", move |b| b.iter(|| digest.update(&bytes)))
18-
.throughput(Throughput::Bytes(1_000_000)),
19-
);
16+
let bytes = vec![0u8; INPUT_SIZE];
17+
let mut group = c.benchmark_group("crc8");
18+
group
19+
.throughput(Throughput::Bytes(bytes.len() as u64))
20+
.bench_function("crc8", move |b| b.iter(|| digest.update(&bytes)));
2021
}
2122

2223
fn crc16(c: &mut Criterion) {
2324
let mut digest = X25.digest();
24-
let bytes = vec![0u8; 1_000_000];
25-
c.bench(
26-
"crc16",
27-
Benchmark::new("crc16", move |b| b.iter(|| digest.update(&bytes)))
28-
.throughput(Throughput::Bytes(1_000_000)),
29-
);
25+
let bytes = vec![0u8; INPUT_SIZE];
26+
let mut group = c.benchmark_group("crc16");
27+
group
28+
.throughput(Throughput::Bytes(bytes.len() as u64))
29+
.bench_function("crc16", move |b| b.iter(|| digest.update(&bytes)));
3030
}
3131

3232
fn crc32(c: &mut Criterion) {
3333
let mut digest = CASTAGNOLI.digest();
34-
let bytes = vec![0u8; 1_000_000];
35-
c.bench(
36-
"crc32",
37-
Benchmark::new("crc32", move |b| b.iter(|| digest.update(&bytes)))
38-
.throughput(Throughput::Bytes(1_000_000)),
39-
);
34+
let bytes = vec![0u8; INPUT_SIZE];
35+
let mut group = c.benchmark_group("crc32");
36+
group
37+
.throughput(Throughput::Bytes(bytes.len() as u64))
38+
.bench_function("crc32", move |b| b.iter(|| digest.update(&bytes)));
4039
}
4140

4241
fn crc40(c: &mut Criterion) {
4342
let mut digest = GSM_40.digest();
44-
let bytes = vec![0u8; 1_000_000];
45-
c.bench(
46-
"crc40",
47-
Benchmark::new("crc40", move |b| b.iter(|| digest.update(&bytes)))
48-
.throughput(Throughput::Bytes(1_000_000)),
49-
);
43+
let bytes = vec![0u8; INPUT_SIZE];
44+
let mut group = c.benchmark_group("crc40");
45+
group
46+
.throughput(Throughput::Bytes(bytes.len() as u64))
47+
.bench_function("crc40", move |b| b.iter(|| digest.update(&bytes)));
5048
}
5149

5250
fn crc64(c: &mut Criterion) {
5351
let mut digest = ECMA.digest();
54-
let bytes = vec![0u8; 1_000_000];
55-
c.bench(
56-
"crc64",
57-
Benchmark::new("crc64", move |b| b.iter(|| digest.update(&bytes)))
58-
.throughput(Throughput::Bytes(1_000_000)),
59-
);
52+
let bytes = vec![0u8; INPUT_SIZE];
53+
let mut group = c.benchmark_group("crc64");
54+
group
55+
.throughput(Throughput::Bytes(bytes.len() as u64))
56+
.bench_function("crc64", move |b| b.iter(|| digest.update(&bytes)));
6057
}
6158

6259
fn crc82(c: &mut Criterion) {
6360
let mut digest = ECMA.digest();
64-
let bytes = vec![0u8; 1_000_000];
65-
c.bench(
66-
"crc82",
67-
Benchmark::new("crc82", move |b| b.iter(|| digest.update(&bytes)))
68-
.throughput(Throughput::Bytes(1_000_000)),
69-
);
61+
let bytes = vec![0u8; INPUT_SIZE];
62+
let mut group = c.benchmark_group("crc82");
63+
group
64+
.throughput(Throughput::Bytes(bytes.len() as u64))
65+
.bench_function("crc82", move |b| b.iter(|| digest.update(&bytes)));
7066
}
7167

7268
criterion_group!(crc8_benches, crc8);

0 commit comments

Comments
 (0)