|
7 | 7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 |
| -use test::Bencher; |
| 10 | +use test::{Bencher, black_box}; |
11 | 11 |
|
12 | 12 | use core::hash::{Hash, Hasher};
|
13 | 13 | use core::hash::SipHasher;
|
@@ -57,6 +57,12 @@ fn hash_with_keys<T: Hash>(k1: u64, k2: u64, x: &T) -> u64 {
|
57 | 57 | st.finish()
|
58 | 58 | }
|
59 | 59 |
|
| 60 | +fn hash_bytes(x: &[u8]) -> u64 { |
| 61 | + let mut s = SipHasher::default(); |
| 62 | + Hasher::write(&mut s, x); |
| 63 | + s.finish() |
| 64 | +} |
| 65 | + |
60 | 66 | #[test]
|
61 | 67 | #[allow(unused_must_use)]
|
62 | 68 | fn test_siphash() {
|
@@ -266,10 +272,88 @@ officia deserunt mollit anim id est laborum.";
|
266 | 272 | })
|
267 | 273 | }
|
268 | 274 |
|
| 275 | +#[bench] |
| 276 | +fn bench_u32(b: &mut Bencher) { |
| 277 | + let u = 162629500u32; |
| 278 | + let u = black_box(u); |
| 279 | + b.iter(|| { |
| 280 | + hash(&u) |
| 281 | + }); |
| 282 | + b.bytes = 8; |
| 283 | +} |
| 284 | + |
| 285 | +#[bench] |
| 286 | +fn bench_u32_keyed(b: &mut Bencher) { |
| 287 | + let u = 162629500u32; |
| 288 | + let u = black_box(u); |
| 289 | + let k1 = black_box(0x1); |
| 290 | + let k2 = black_box(0x2); |
| 291 | + b.iter(|| { |
| 292 | + hash_with_keys(k1, k2, &u) |
| 293 | + }); |
| 294 | + b.bytes = 8; |
| 295 | +} |
| 296 | + |
269 | 297 | #[bench]
|
270 | 298 | fn bench_u64(b: &mut Bencher) {
|
271 | 299 | let u = 16262950014981195938u64;
|
| 300 | + let u = black_box(u); |
272 | 301 | b.iter(|| {
|
273 |
| - assert_eq!(hash(&u), 5254097107239593357); |
274 |
| - }) |
| 302 | + hash(&u) |
| 303 | + }); |
| 304 | + b.bytes = 8; |
| 305 | +} |
| 306 | + |
| 307 | +#[bench] |
| 308 | +fn bench_bytes_4(b: &mut Bencher) { |
| 309 | + let data = black_box([b' '; 4]); |
| 310 | + b.iter(|| { |
| 311 | + hash_bytes(&data) |
| 312 | + }); |
| 313 | + b.bytes = 4; |
| 314 | +} |
| 315 | + |
| 316 | +#[bench] |
| 317 | +fn bench_bytes_7(b: &mut Bencher) { |
| 318 | + let data = black_box([b' '; 7]); |
| 319 | + b.iter(|| { |
| 320 | + hash_bytes(&data) |
| 321 | + }); |
| 322 | + b.bytes = 7; |
| 323 | +} |
| 324 | + |
| 325 | +#[bench] |
| 326 | +fn bench_bytes_8(b: &mut Bencher) { |
| 327 | + let data = black_box([b' '; 8]); |
| 328 | + b.iter(|| { |
| 329 | + hash_bytes(&data) |
| 330 | + }); |
| 331 | + b.bytes = 8; |
| 332 | +} |
| 333 | + |
| 334 | +#[bench] |
| 335 | +fn bench_bytes_a_16(b: &mut Bencher) { |
| 336 | + let data = black_box([b' '; 16]); |
| 337 | + b.iter(|| { |
| 338 | + hash_bytes(&data) |
| 339 | + }); |
| 340 | + b.bytes = 16; |
| 341 | +} |
| 342 | + |
| 343 | +#[bench] |
| 344 | +fn bench_bytes_b_32(b: &mut Bencher) { |
| 345 | + let data = black_box([b' '; 32]); |
| 346 | + b.iter(|| { |
| 347 | + hash_bytes(&data) |
| 348 | + }); |
| 349 | + b.bytes = 32; |
| 350 | +} |
| 351 | + |
| 352 | +#[bench] |
| 353 | +fn bench_bytes_c_128(b: &mut Bencher) { |
| 354 | + let data = black_box([b' '; 128]); |
| 355 | + b.iter(|| { |
| 356 | + hash_bytes(&data) |
| 357 | + }); |
| 358 | + b.bytes = 128; |
275 | 359 | }
|
0 commit comments