• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crc::*;
2 use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
3 
4 pub const BLUETOOTH: Crc<u8> = Crc::<u8>::new(&CRC_8_BLUETOOTH);
5 pub const BLUETOOTH_SLICE16: Crc<u8, Table<16>> = Crc::<u8, Table<16>>::new(&CRC_8_BLUETOOTH);
6 pub const BLUETOOTH_BYTEWISE: Crc<u8, Table<1>> = Crc::<u8, Table<1>>::new(&CRC_8_BLUETOOTH);
7 pub const BLUETOOTH_NOLOOKUP: Crc<u8, NoTable> = Crc::<u8, NoTable>::new(&CRC_8_BLUETOOTH);
8 pub const X25: Crc<u16> = Crc::<u16>::new(&CRC_16_IBM_SDLC);
9 pub const X25_SLICE16: Crc<u16, Table<16>> = Crc::<u16, Table<16>>::new(&CRC_16_IBM_SDLC);
10 pub const X25_BYTEWISE: Crc<u16, Table<1>> = Crc::<u16, Table<1>>::new(&CRC_16_IBM_SDLC);
11 pub const X25_NOLOOKUP: Crc<u16, NoTable> = Crc::<u16, NoTable>::new(&CRC_16_IBM_SDLC);
12 pub const ISCSI: Crc<u32> = Crc::<u32>::new(&CRC_32_ISCSI);
13 pub const ISCSI_SLICE16: Crc<u32, Table<16>> = Crc::<u32, Table<16>>::new(&CRC_32_ISCSI);
14 pub const ISCSI_BYTEWISE: Crc<u32, Table<1>> = Crc::<u32, Table<1>>::new(&CRC_32_ISCSI);
15 pub const ISCSI_NOLOOKUP: Crc<u32, NoTable> = Crc::<u32, NoTable>::new(&CRC_32_ISCSI);
16 pub const GSM_40: Crc<u64> = Crc::<u64>::new(&CRC_40_GSM);
17 pub const ECMA: Crc<u64> = Crc::<u64>::new(&CRC_64_ECMA_182);
18 pub const ECMA_SLICE16: Crc<u64, Table<16>> = Crc::<u64, Table<16>>::new(&CRC_64_ECMA_182);
19 pub const ECMA_BYTEWISE: Crc<u64, Table<1>> = Crc::<u64, Table<1>>::new(&CRC_64_ECMA_182);
20 pub const ECMA_NOLOOKUP: Crc<u64, NoTable> = Crc::<u64, NoTable>::new(&CRC_64_ECMA_182);
21 pub const DARC: Crc<u128> = Crc::<u128>::new(&CRC_82_DARC);
22 pub const DARC_SLICE16: Crc<u128, Table<16>> = Crc::<u128, Table<16>>::new(&CRC_82_DARC);
23 pub const DARC_BYTEWISE: Crc<u128, Table<1>> = Crc::<u128, Table<1>>::new(&CRC_82_DARC);
24 pub const DARC_NOLOOKUP: Crc<u128, NoTable> = Crc::<u128, NoTable>::new(&CRC_82_DARC);
25 
26 static KB: usize = 1024;
27 
baseline(data: &[u8]) -> usize28 fn baseline(data: &[u8]) -> usize {
29     data.iter()
30         .fold(0usize, |acc, v| acc.wrapping_add(*v as usize))
31 }
32 
checksum(c: &mut Criterion)33 fn checksum(c: &mut Criterion) {
34     let size = 16 * KB;
35     let bytes = vec![0u8; size];
36 
37     c.benchmark_group("baseline")
38         .throughput(Throughput::Bytes(size as u64))
39         .bench_function("baseline", |b| b.iter(|| baseline(black_box(&bytes))));
40 
41     c.benchmark_group("crc8")
42         .throughput(Throughput::Bytes(size as u64))
43         .bench_function("default", |b| {
44             b.iter(|| BLUETOOTH.checksum(black_box(&bytes)))
45         })
46         .bench_function("nolookup", |b| {
47             b.iter(|| BLUETOOTH_NOLOOKUP.checksum(black_box(&bytes)))
48         })
49         .bench_function("bytewise", |b| {
50             b.iter(|| BLUETOOTH_BYTEWISE.checksum(black_box(&bytes)))
51         })
52         .bench_function("slice16", |b| {
53             b.iter(|| BLUETOOTH_SLICE16.checksum(black_box(&bytes)))
54         });
55 
56     c.benchmark_group("crc16")
57         .throughput(Throughput::Bytes(size as u64))
58         .bench_function("default", |b| b.iter(|| X25.checksum(black_box(&bytes))))
59         .bench_function("nolookup", |b| {
60             b.iter(|| X25_NOLOOKUP.checksum(black_box(&bytes)))
61         })
62         .bench_function("bytewise", |b| {
63             b.iter(|| X25_BYTEWISE.checksum(black_box(&bytes)))
64         })
65         .bench_function("slice16", |b| {
66             b.iter(|| X25_SLICE16.checksum(black_box(&bytes)))
67         });
68 
69     c.benchmark_group("crc32")
70         .throughput(Throughput::Bytes(size as u64))
71         .bench_function("default", |b| b.iter(|| ISCSI.checksum(black_box(&bytes))))
72         .bench_function("nolookup", |b| {
73             b.iter(|| ISCSI_NOLOOKUP.checksum(black_box(&bytes)))
74         })
75         .bench_function("bytewise", |b| {
76             b.iter(|| ISCSI_BYTEWISE.checksum(black_box(&bytes)))
77         })
78         .bench_function("slice16", |b| {
79             b.iter(|| ISCSI_SLICE16.checksum(black_box(&bytes)))
80         });
81 
82     c.benchmark_group("crc64")
83         .throughput(Throughput::Bytes(size as u64))
84         .bench_function("default", |b| b.iter(|| ECMA.checksum(black_box(&bytes))))
85         .bench_function("nolookup", |b| {
86             b.iter(|| ECMA_NOLOOKUP.checksum(black_box(&bytes)))
87         })
88         .bench_function("bytewise", |b| {
89             b.iter(|| ECMA_BYTEWISE.checksum(black_box(&bytes)))
90         })
91         .bench_function("slice16", |b| {
92             b.iter(|| ECMA_SLICE16.checksum(black_box(&bytes)))
93         });
94 
95     c.benchmark_group("crc82")
96         .throughput(Throughput::Bytes(size as u64))
97         .bench_function("default", |b| b.iter(|| DARC.checksum(black_box(&bytes))))
98         .bench_function("nolookup", |b| {
99             b.iter(|| DARC_NOLOOKUP.checksum(black_box(&bytes)))
100         })
101         .bench_function("bytewise", |b| {
102             b.iter(|| DARC_BYTEWISE.checksum(black_box(&bytes)))
103         })
104         .bench_function("slice16", |b| {
105             b.iter(|| DARC_SLICE16.checksum(black_box(&bytes)))
106         });
107 
108     c.benchmark_group("checksum")
109         .bench_function("crc8", |b| b.iter(|| BLUETOOTH.checksum(black_box(&bytes))))
110         .bench_function("crc40", |b| b.iter(|| GSM_40.checksum(black_box(&bytes))));
111 }
112 
113 criterion_group!(checksum_benches, checksum);
114 criterion_main!(checksum_benches);
115