2017-02-03 17:04:22 -06:00
|
|
|
#![allow(deprecated)]
|
|
|
|
|
|
|
|
use core::hash::*;
|
2019-12-06 22:18:12 -06:00
|
|
|
use test::{black_box, Bencher};
|
2017-02-03 17:04:22 -06:00
|
|
|
|
|
|
|
fn hash_bytes<H: Hasher>(mut s: H, x: &[u8]) -> u64 {
|
|
|
|
Hasher::write(&mut s, x);
|
|
|
|
s.finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hash_with<H: Hasher, T: Hash>(mut st: H, x: &T) -> u64 {
|
|
|
|
x.hash(&mut st);
|
|
|
|
st.finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hash<T: Hash>(x: &T) -> u64 {
|
|
|
|
hash_with(SipHasher::new(), x)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_str_under_8_bytes(b: &mut Bencher) {
|
|
|
|
let s = "foo";
|
|
|
|
b.iter(|| {
|
|
|
|
assert_eq!(hash(&s), 16262950014981195938);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_str_of_8_bytes(b: &mut Bencher) {
|
|
|
|
let s = "foobar78";
|
|
|
|
b.iter(|| {
|
|
|
|
assert_eq!(hash(&s), 4898293253460910787);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_str_over_8_bytes(b: &mut Bencher) {
|
|
|
|
let s = "foobarbaz0";
|
|
|
|
b.iter(|| {
|
|
|
|
assert_eq!(hash(&s), 10581415515220175264);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_long_str(b: &mut Bencher) {
|
|
|
|
let s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor \
|
2019-12-06 22:18:12 -06:00
|
|
|
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud \
|
|
|
|
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute \
|
|
|
|
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \
|
|
|
|
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui \
|
|
|
|
officia deserunt mollit anim id est laborum.";
|
2017-02-03 17:04:22 -06:00
|
|
|
b.iter(|| {
|
|
|
|
assert_eq!(hash(&s), 17717065544121360093);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_u32(b: &mut Bencher) {
|
|
|
|
let u = 162629500u32;
|
|
|
|
let u = black_box(u);
|
2019-12-06 22:18:12 -06:00
|
|
|
b.iter(|| hash(&u));
|
2017-02-03 17:04:22 -06:00
|
|
|
b.bytes = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_u32_keyed(b: &mut Bencher) {
|
|
|
|
let u = 162629500u32;
|
|
|
|
let u = black_box(u);
|
|
|
|
let k1 = black_box(0x1);
|
|
|
|
let k2 = black_box(0x2);
|
2019-12-06 22:18:12 -06:00
|
|
|
b.iter(|| hash_with(SipHasher::new_with_keys(k1, k2), &u));
|
2017-02-03 17:04:22 -06:00
|
|
|
b.bytes = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_u64(b: &mut Bencher) {
|
|
|
|
let u = 16262950014981195938u64;
|
|
|
|
let u = black_box(u);
|
2019-12-06 22:18:12 -06:00
|
|
|
b.iter(|| hash(&u));
|
2017-02-03 17:04:22 -06:00
|
|
|
b.bytes = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_bytes_4(b: &mut Bencher) {
|
|
|
|
let data = black_box([b' '; 4]);
|
2019-12-06 22:18:12 -06:00
|
|
|
b.iter(|| hash_bytes(SipHasher::default(), &data));
|
2017-02-03 17:04:22 -06:00
|
|
|
b.bytes = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_bytes_7(b: &mut Bencher) {
|
|
|
|
let data = black_box([b' '; 7]);
|
2019-12-06 22:18:12 -06:00
|
|
|
b.iter(|| hash_bytes(SipHasher::default(), &data));
|
2017-02-03 17:04:22 -06:00
|
|
|
b.bytes = 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_bytes_8(b: &mut Bencher) {
|
|
|
|
let data = black_box([b' '; 8]);
|
2019-12-06 22:18:12 -06:00
|
|
|
b.iter(|| hash_bytes(SipHasher::default(), &data));
|
2017-02-03 17:04:22 -06:00
|
|
|
b.bytes = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_bytes_a_16(b: &mut Bencher) {
|
|
|
|
let data = black_box([b' '; 16]);
|
2019-12-06 22:18:12 -06:00
|
|
|
b.iter(|| hash_bytes(SipHasher::default(), &data));
|
2017-02-03 17:04:22 -06:00
|
|
|
b.bytes = 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_bytes_b_32(b: &mut Bencher) {
|
|
|
|
let data = black_box([b' '; 32]);
|
2019-12-06 22:18:12 -06:00
|
|
|
b.iter(|| hash_bytes(SipHasher::default(), &data));
|
2017-02-03 17:04:22 -06:00
|
|
|
b.bytes = 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_bytes_c_128(b: &mut Bencher) {
|
|
|
|
let data = black_box([b' '; 128]);
|
2019-12-06 22:18:12 -06:00
|
|
|
b.iter(|| hash_bytes(SipHasher::default(), &data));
|
2017-02-03 17:04:22 -06:00
|
|
|
b.bytes = 128;
|
|
|
|
}
|