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.
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.
14 use test::{Bencher, black_box};
16 fn hash_bytes<H: Hasher>(mut s: H, x: &[u8]) -> u64 {
17 Hasher::write(&mut s, x);
21 fn hash_with<H: Hasher, T: Hash>(mut st: H, x: &T) -> u64 {
26 fn hash<T: Hash>(x: &T) -> u64 {
27 hash_with(SipHasher::new(), x)
31 fn bench_str_under_8_bytes(b: &mut Bencher) {
34 assert_eq!(hash(&s), 16262950014981195938);
39 fn bench_str_of_8_bytes(b: &mut Bencher) {
42 assert_eq!(hash(&s), 4898293253460910787);
47 fn bench_str_over_8_bytes(b: &mut Bencher) {
50 assert_eq!(hash(&s), 10581415515220175264);
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.";
63 assert_eq!(hash(&s), 17717065544121360093);
68 fn bench_u32(b: &mut Bencher) {
78 fn bench_u32_keyed(b: &mut Bencher) {
81 let k1 = black_box(0x1);
82 let k2 = black_box(0x2);
84 hash_with(SipHasher::new_with_keys(k1, k2), &u)
90 fn bench_u64(b: &mut Bencher) {
91 let u = 16262950014981195938u64;
100 fn bench_bytes_4(b: &mut Bencher) {
101 let data = black_box([b' '; 4]);
103 hash_bytes(SipHasher::default(), &data)
109 fn bench_bytes_7(b: &mut Bencher) {
110 let data = black_box([b' '; 7]);
112 hash_bytes(SipHasher::default(), &data)
118 fn bench_bytes_8(b: &mut Bencher) {
119 let data = black_box([b' '; 8]);
121 hash_bytes(SipHasher::default(), &data)
127 fn bench_bytes_a_16(b: &mut Bencher) {
128 let data = black_box([b' '; 16]);
130 hash_bytes(SipHasher::default(), &data)
136 fn bench_bytes_b_32(b: &mut Bencher) {
137 let data = black_box([b' '; 32]);
139 hash_bytes(SipHasher::default(), &data)
145 fn bench_bytes_c_128(b: &mut Bencher) {
146 let data = black_box([b' '; 128]);
148 hash_bytes(SipHasher::default(), &data)