|
| 1 | +#![feature(test)] |
| 2 | + |
| 3 | +extern crate test; |
| 4 | +extern crate rand; |
| 5 | +extern crate rand_core; |
| 6 | + |
| 7 | +use std::mem::{size_of, size_of_val}; |
| 8 | +use test::{black_box, Bencher}; |
| 9 | +use rand::{Rng, thread_rng, random}; |
| 10 | +use rand::hash::*; |
| 11 | + |
| 12 | +#[bench] |
| 13 | +fn hash_u64(b: &mut Bencher) { |
| 14 | + const N: u64 = 100; |
| 15 | + let mut x: u64 = random(); |
| 16 | + b.iter(|| { |
| 17 | + for _ in 0..N { |
| 18 | + x = x.wrapping_add(1); // unique number each time |
| 19 | + |
| 20 | + // Using `hash` instead of `hash_u64` here has big performance impact — why? |
| 21 | + black_box(SeaHash::new_fixed().hash_u64(x)); |
| 22 | + //black_box(SeaHash::new_fixed().hash(x)); |
| 23 | + //black_box(Hashable::hash(SeaHash::new_fixed(), x)); |
| 24 | + } |
| 25 | + }); |
| 26 | + b.bytes = size_of::<u64>() as u64 * N; |
| 27 | +} |
| 28 | + |
| 29 | +#[bench] |
| 30 | +fn hash_bytes32(b: &mut Bencher) { |
| 31 | + const N: u64 = 20; |
| 32 | + let mut buf = [0u8; 32]; |
| 33 | + thread_rng().fill_bytes(&mut buf); |
| 34 | + b.iter(|| { |
| 35 | + for _ in 0..N { |
| 36 | + buf[0] = buf[0].wrapping_add(1); // unique number each time |
| 37 | + black_box(SeaHash::hash_fixed(&buf[..])); |
| 38 | + } |
| 39 | + }); |
| 40 | + b.bytes = size_of_val(&buf) as u64 * N; |
| 41 | +} |
| 42 | + |
| 43 | +#[bench] |
| 44 | +fn hash_bytes1024(b: &mut Bencher) { |
| 45 | + const N: u64 = 1; |
| 46 | + let mut buf = [0u8; 1024]; |
| 47 | + thread_rng().fill_bytes(&mut buf); |
| 48 | + b.iter(|| { |
| 49 | + for _ in 0..N { |
| 50 | + buf[0] = buf[0].wrapping_add(1); // unique number each time |
| 51 | + black_box(SeaHash::hash_fixed(&buf[..])); |
| 52 | + } |
| 53 | + }); |
| 54 | + b.bytes = size_of_val(&buf) as u64 * N; |
| 55 | +} |
0 commit comments