summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDevin Ragotzy <[email protected]>2021-11-02 21:11:37 -0400
committerDevin Ragotzy <[email protected]>2021-11-02 21:11:37 -0400
commitc638ecf35d17bba9c7634ea2c4ec4126c61a9705 (patch)
treeefd9e9efe92a8b1b56ef789ef4d372926c911a48
parent73b1ed7c17b94647438c7e73738bf0820e5d57ad (diff)
downloadSTC-modified-c638ecf35d17bba9c7634ea2c4ec4126c61a9705.tar.gz
STC-modified-c638ecf35d17bba9c7634ea2c4ec4126c61a9705.zip
Bring rust benchmarks in line with c benchmarks
-rw-r--r--benchmarks/rust_hashmap.rs135
1 files changed, 91 insertions, 44 deletions
diff --git a/benchmarks/rust_hashmap.rs b/benchmarks/rust_hashmap.rs
index d1372e8b..8e9a5250 100644
--- a/benchmarks/rust_hashmap.rs
+++ b/benchmarks/rust_hashmap.rs
@@ -1,44 +1,91 @@
-use std::time::{Instant};
-use std::io::Read;
-
-fn romu_rotl(val: u64, r: u32) -> u64
- { return (val << r) | (val >> (64 - r)); }
-
-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);
- 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 mut rng: [u64; 3] = [1872361123, 123879177, 87739234];
- println!("Rust HashMap n = {}, mask = {:#x}", n, mask);
- let now = Instant::now();
- for _i in 0..n {
- 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;
- for i in 0..mask + 1 { if m.contains_key(&i) { sum += 1; }}
- println!("lookup : {}ms \tsum : {}", now.elapsed().as_millis(), sum);
-
- let now = Instant::now();
- let mut sum = 0;
- for (_, value) in &m { sum += value; }
- println!("iterate : {}ms \tsum : {}", now.elapsed().as_millis(), sum);
-
- let mut rng: [u64; 3] = [1872361123, 123879177, 87739234];
- let now = Instant::now();
- for _ in 0..n {
- let key: u64 = romu_trio(&mut rng) & mask;
- m.remove(&key);
- }
- println!("remove : {}ms \tsize : {}", now.elapsed().as_millis(), m.len());
- println!("press a key:"); std::io::stdin().bytes().next();
-} \ No newline at end of file
+use std::{
+ hash::{BuildHasherDefault, Hasher},
+ io::Read,
+ time::Instant,
+};
+
+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))
+// }
+
+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, 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();
+ for _i in 0..n {
+ 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;
+ for i in 0..mask + 1 { if m.contains_key(&i) { sum += 1; }}
+ println!("lookup : {}ms \tsum : {}", now.elapsed().as_millis(), sum);
+
+ let now = Instant::now();
+ let mut sum = 0;
+ for (_, value) in &m { sum += value; }
+ println!("iterate : {}ms \tsum : {}", now.elapsed().as_millis(), sum);
+
+ let mut rng: [u64; 3] = [1872361123, 123879177, 87739234];
+ let now = Instant::now();
+ for _ in 0..n {
+ let key: u64 = romu_trio(&mut rng) & mask;
+ m.remove(&key);
+ }
+ println!("remove : {}ms \tsize : {}", now.elapsed().as_millis(), m.len());
+ println!("press a key:");
+ std::io::stdin().bytes().next();
+}