1 #[macro_use]
2 extern crate bencher;
3 extern crate crc32fast;
4 extern crate rand;
5
6 use bencher::Bencher;
7 use crc32fast::Hasher;
8 use rand::Rng;
9
bench(b: &mut Bencher, size: usize, hasher_init: Hasher)10 fn bench(b: &mut Bencher, size: usize, hasher_init: Hasher) {
11 let mut bytes = vec![0u8; size];
12 rand::thread_rng().fill(&mut bytes[..]);
13
14 b.iter(|| {
15 let mut hasher = hasher_init.clone();
16 hasher.update(&bytes);
17 bencher::black_box(hasher.finalize())
18 });
19
20 b.bytes = size as u64;
21 }
22
bench_kilobyte_baseline(b: &mut Bencher)23 fn bench_kilobyte_baseline(b: &mut Bencher) {
24 bench(b, 1024, Hasher::internal_new_baseline(0))
25 }
26
bench_kilobyte_specialized(b: &mut Bencher)27 fn bench_kilobyte_specialized(b: &mut Bencher) {
28 bench(b, 1024, Hasher::internal_new_specialized(0).unwrap())
29 }
30
bench_megabyte_baseline(b: &mut Bencher)31 fn bench_megabyte_baseline(b: &mut Bencher) {
32 bench(b, 1024 * 1024, Hasher::internal_new_baseline(0))
33 }
34
bench_megabyte_specialized(b: &mut Bencher)35 fn bench_megabyte_specialized(b: &mut Bencher) {
36 bench(b, 1024 * 1024, Hasher::internal_new_specialized(0).unwrap())
37 }
38
39 benchmark_group!(
40 bench_baseline,
41 bench_kilobyte_baseline,
42 bench_megabyte_baseline
43 );
44 benchmark_group!(
45 bench_specialized,
46 bench_kilobyte_specialized,
47 bench_megabyte_specialized
48 );
49 benchmark_main!(bench_baseline, bench_specialized);
50