summaryrefslogtreecommitdiffhomepage
path: root/benchmarks/misc
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-11-12 10:02:53 +0100
committerTyge Løvset <[email protected]>2021-11-12 10:02:53 +0100
commit5396248d15ace0e8837f87f4a4b815a086b4d206 (patch)
treedf711700ef355825c2f3a22e197793a186551830 /benchmarks/misc
parent36c1ed31c7376a48d60ee709e84f7aa1acaf0867 (diff)
parentc638ecf35d17bba9c7634ea2c4ec4126c61a9705 (diff)
downloadSTC-modified-5396248d15ace0e8837f87f4a4b815a086b4d206.tar.gz
STC-modified-5396248d15ace0e8837f87f4a4b815a086b4d206.zip
Accepted pull request on rust_hashmap.rs
Diffstat (limited to 'benchmarks/misc')
-rw-r--r--benchmarks/misc/rust_hashmap.rs74
1 files changed, 60 insertions, 14 deletions
diff --git a/benchmarks/misc/rust_hashmap.rs b/benchmarks/misc/rust_hashmap.rs
index cd1f114e..2e042449 100644
--- a/benchmarks/misc/rust_hashmap.rs
+++ b/benchmarks/misc/rust_hashmap.rs
@@ -1,20 +1,66 @@
-use std::time::{Instant};
-use std::io::Read;
+use std::{
+ hash::{BuildHasherDefault, Hasher},
+ io::Read,
+ time::Instant,
+};
-fn romu_rotl(val: u64, r: u32) -> u64
- { return (val << r) | (val >> (64 - r)); }
+struct MyHasher {
+ seed: u64,
+}
+
+impl Default for MyHasher {
+ fn default() -> Self {
+ Self { seed: 0xb5ad4eceda1ce2a9_u64 }
+ }
+}
+
+// fn rotl(h: u64, y: u64) -> u64 {
+// (h << y) | (h >> (64 - y))
+// }
-fn romu_trio(s: &mut[u64]) -> u64 {
- let xp = s[0]; let yp = s[1]; let zp = s[2];
- s[0] = 15241094284759029579 * zp;
- s[1] = yp - xp; s[1] = romu_rotl(s[1], 12);
- s[2] = zp - yp; s[2] = romu_rotl(s[2], 44);
+impl Hasher for MyHasher {
+ fn write(&mut self, bytes: &[u8]) {
+ use std::convert::TryInto;
+ self.seed = u64::from_ne_bytes(bytes.try_into().unwrap()).wrapping_mul(0xc6a4a7935bd1e99d);
+ }
+
+ #[inline]
+ fn write_u64(&mut self, i: u64) {
+ self.seed = i.wrapping_mul(0xc6a4a7935bd1e99d);
+ }
+
+ #[inline]
+ fn finish(&self) -> u64 {
+ self.seed
+ }
+}
+
+type MyBuildHasher = BuildHasherDefault<MyHasher>;
+
+#[inline]
+fn romu_rotl(val: u64, r: u32) -> u64 {
+ return (val.wrapping_shl(r)).wrapping_add(val.wrapping_shr(64_u32.wrapping_sub(r)));
+}
+
+fn romu_trio(s: &mut [u64]) -> u64 {
+ let xp = s[0];
+ let yp = s[1];
+ let zp = s[2];
+ s[0] = 15241094284759029579_u64.wrapping_mul(zp);
+ s[1] = yp.wrapping_sub(xp);
+ s[1] = romu_rotl(s[1], 12);
+ s[2] = zp.wrapping_sub(yp);
+ s[2] = romu_rotl(s[2], 44);
return xp;
}
fn main() {
- let n = 50_000_000; let mask = (1 << 25) - 1;
- let mut m = std::collections::HashMap::<u64, u64>::with_capacity(n);
+ let n = 50_000_000;
+ let mask = (1 << 25) - 1;
+
+ let mut m = std::collections::HashMap::<u64, u64, MyBuildHasher>::default();
+ m.reserve(n);
+
let mut rng: [u64; 3] = [1872361123, 123879177, 87739234];
println!("Rust HashMap n = {}, mask = {:#x}", n, mask);
let now = Instant::now();
@@ -22,7 +68,6 @@ fn main() {
let key: u64 = romu_trio(&mut rng) & mask;
*m.entry(key).or_insert(0) += 1;
}
-
println!("insert : {}ms \tsize : {}", now.elapsed().as_millis(), m.len());
let now = Instant::now();
let mut sum = 0;
@@ -41,5 +86,6 @@ fn main() {
m.remove(&key);
}
println!("remove : {}ms \tsize : {}", now.elapsed().as_millis(), m.len());
- println!("press a key:"); std::io::stdin().bytes().next();
-}
+ println!("press a key:");
+ std::io::stdin().bytes().next();
+} \ No newline at end of file