use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; const EXTERN_CRC: crc::Crc = crc::Crc::::new(&crc::CRC_32_ISO_HDLC); #[inline] fn do_extern_crc(bytes: &[u8]) -> u32 { let mut digest = EXTERN_CRC.digest(); digest.update(bytes); digest.finalize() } #[inline] fn do_extern_crc32fast(bytes: &[u8]) -> u32 { let mut digest = crc32fast::Hasher::new(); digest.update(bytes); digest.finalize() } const REFERENCE: fcrc::reference::Crc = fcrc::reference::Crc::::new(fcrc::models::CRC_32_ISO_HDLC); #[inline] fn do_reference(bytes: &[u8]) -> u32 { REFERENCE.checksum(bytes) } const SL1: fcrc::slice_by::Crc = fcrc::slice_by::Crc::::new(fcrc::models::CRC_32_ISO_HDLC); #[inline] fn do_slice_by_1(bytes: &[u8]) -> u32 { SL1.checksum(bytes) } const SL8: fcrc::slice_by::Crc = fcrc::slice_by::Crc::::new(fcrc::models::CRC_32_ISO_HDLC); #[inline] fn do_slice_by_8(bytes: &[u8]) -> u32 { SL8.checksum(bytes) } const SL16: fcrc::slice_by::Crc = fcrc::slice_by::Crc::::new(fcrc::models::CRC_32_ISO_HDLC); #[inline] fn do_slice_by_16(bytes: &[u8]) -> u32 { SL16.checksum(bytes) } lazy_static::lazy_static! { static ref PCLMULQDQ: fcrc::pclmulqdq::Crc = fcrc::pclmulqdq::Crc::::new(fcrc::models::CRC_32_ISO_HDLC).unwrap(); } #[inline] fn do_pclmulqdq(bytes: &[u8]) -> u32 { PCLMULQDQ.checksum(bytes) } fn compare_crc(c: &mut Criterion) { let mut group = c.benchmark_group("crc32"); for dbl in [0, 2] { let size = 1024usize << dbl; group.throughput(Throughput::Bytes(size as u64)); let mut bytes = Vec::with_capacity(size); for _ in 0..size { bytes.push(fastrand::u8(..)); } group.bench_with_input( BenchmarkId::new("extern crate crc", size), &bytes[..], |b, bytes| { b.iter(|| { black_box(do_extern_crc(bytes)); }); }, ); group.bench_with_input( BenchmarkId::new("reference", size), &bytes[..], |b, bytes| { b.iter(|| { black_box(do_reference(bytes)); }); }, ); group.bench_with_input( BenchmarkId::new("slice_by_1", size), &bytes[..], |b, bytes| { b.iter(|| { black_box(do_slice_by_1(bytes)); }); }, ); group.bench_with_input( BenchmarkId::new("slice_by_8", size), &bytes[..], |b, bytes| { b.iter(|| { black_box(do_slice_by_8(bytes)); }); }, ); group.bench_with_input( BenchmarkId::new("slice_by_16", size), &bytes[..], |b, bytes| { b.iter(|| { black_box(do_slice_by_16(bytes)); }); }, ); group.bench_with_input( BenchmarkId::new("pclmulqdq", size), &bytes[..], |b, bytes| { b.iter(|| { black_box(do_pclmulqdq(bytes)); }); }, ); group.bench_with_input( BenchmarkId::new("extern crate crc32fast", size), &bytes[..], |b, bytes| { b.iter(|| { black_box(do_extern_crc32fast(bytes)); }); }, ); } group.finish(); } criterion_group!(benches, compare_crc); criterion_main!(benches);