|
| 1 | +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +#![allow(deprecated)] |
| 12 | + |
| 13 | +use core::hash::*; |
| 14 | +use test::{Bencher, black_box}; |
| 15 | + |
| 16 | +fn hash_bytes<H: Hasher>(mut s: H, x: &[u8]) -> u64 { |
| 17 | + Hasher::write(&mut s, x); |
| 18 | + s.finish() |
| 19 | +} |
| 20 | + |
| 21 | +fn hash_with<H: Hasher, T: Hash>(mut st: H, x: &T) -> u64 { |
| 22 | + x.hash(&mut st); |
| 23 | + st.finish() |
| 24 | +} |
| 25 | + |
| 26 | +fn hash<T: Hash>(x: &T) -> u64 { |
| 27 | + hash_with(SipHasher::new(), x) |
| 28 | +} |
| 29 | + |
| 30 | +#[bench] |
| 31 | +fn bench_str_under_8_bytes(b: &mut Bencher) { |
| 32 | + let s = "foo"; |
| 33 | + b.iter(|| { |
| 34 | + assert_eq!(hash(&s), 16262950014981195938); |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +#[bench] |
| 39 | +fn bench_str_of_8_bytes(b: &mut Bencher) { |
| 40 | + let s = "foobar78"; |
| 41 | + b.iter(|| { |
| 42 | + assert_eq!(hash(&s), 4898293253460910787); |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +#[bench] |
| 47 | +fn bench_str_over_8_bytes(b: &mut Bencher) { |
| 48 | + let s = "foobarbaz0"; |
| 49 | + b.iter(|| { |
| 50 | + assert_eq!(hash(&s), 10581415515220175264); |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +#[bench] |
| 55 | +fn bench_long_str(b: &mut Bencher) { |
| 56 | + let s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor \ |
| 57 | +incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud \ |
| 58 | +exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute \ |
| 59 | +irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \ |
| 60 | +pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui \ |
| 61 | +officia deserunt mollit anim id est laborum."; |
| 62 | + b.iter(|| { |
| 63 | + assert_eq!(hash(&s), 17717065544121360093); |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +#[bench] |
| 68 | +fn bench_u32(b: &mut Bencher) { |
| 69 | + let u = 162629500u32; |
| 70 | + let u = black_box(u); |
| 71 | + b.iter(|| { |
| 72 | + hash(&u) |
| 73 | + }); |
| 74 | + b.bytes = 8; |
| 75 | +} |
| 76 | + |
| 77 | +#[bench] |
| 78 | +fn bench_u32_keyed(b: &mut Bencher) { |
| 79 | + let u = 162629500u32; |
| 80 | + let u = black_box(u); |
| 81 | + let k1 = black_box(0x1); |
| 82 | + let k2 = black_box(0x2); |
| 83 | + b.iter(|| { |
| 84 | + hash_with(SipHasher::new_with_keys(k1, k2), &u) |
| 85 | + }); |
| 86 | + b.bytes = 8; |
| 87 | +} |
| 88 | + |
| 89 | +#[bench] |
| 90 | +fn bench_u64(b: &mut Bencher) { |
| 91 | + let u = 16262950014981195938u64; |
| 92 | + let u = black_box(u); |
| 93 | + b.iter(|| { |
| 94 | + hash(&u) |
| 95 | + }); |
| 96 | + b.bytes = 8; |
| 97 | +} |
| 98 | + |
| 99 | +#[bench] |
| 100 | +fn bench_bytes_4(b: &mut Bencher) { |
| 101 | + let data = black_box([b' '; 4]); |
| 102 | + b.iter(|| { |
| 103 | + hash_bytes(SipHasher::default(), &data) |
| 104 | + }); |
| 105 | + b.bytes = 4; |
| 106 | +} |
| 107 | + |
| 108 | +#[bench] |
| 109 | +fn bench_bytes_7(b: &mut Bencher) { |
| 110 | + let data = black_box([b' '; 7]); |
| 111 | + b.iter(|| { |
| 112 | + hash_bytes(SipHasher::default(), &data) |
| 113 | + }); |
| 114 | + b.bytes = 7; |
| 115 | +} |
| 116 | + |
| 117 | +#[bench] |
| 118 | +fn bench_bytes_8(b: &mut Bencher) { |
| 119 | + let data = black_box([b' '; 8]); |
| 120 | + b.iter(|| { |
| 121 | + hash_bytes(SipHasher::default(), &data) |
| 122 | + }); |
| 123 | + b.bytes = 8; |
| 124 | +} |
| 125 | + |
| 126 | +#[bench] |
| 127 | +fn bench_bytes_a_16(b: &mut Bencher) { |
| 128 | + let data = black_box([b' '; 16]); |
| 129 | + b.iter(|| { |
| 130 | + hash_bytes(SipHasher::default(), &data) |
| 131 | + }); |
| 132 | + b.bytes = 16; |
| 133 | +} |
| 134 | + |
| 135 | +#[bench] |
| 136 | +fn bench_bytes_b_32(b: &mut Bencher) { |
| 137 | + let data = black_box([b' '; 32]); |
| 138 | + b.iter(|| { |
| 139 | + hash_bytes(SipHasher::default(), &data) |
| 140 | + }); |
| 141 | + b.bytes = 32; |
| 142 | +} |
| 143 | + |
| 144 | +#[bench] |
| 145 | +fn bench_bytes_c_128(b: &mut Bencher) { |
| 146 | + let data = black_box([b' '; 128]); |
| 147 | + b.iter(|| { |
| 148 | + hash_bytes(SipHasher::default(), &data) |
| 149 | + }); |
| 150 | + b.bytes = 128; |
| 151 | +} |
0 commit comments