use rand instead of file

This commit is contained in:
abbycin 2023-10-08 20:10:24 +08:00
parent 2c8f06a961
commit 4156be9a7a
Signed by untrusted user: abby
GPG Key ID: B636E0F0307EF8EB
3 changed files with 84 additions and 18 deletions

56
Cargo.lock generated
View File

@ -71,12 +71,24 @@ version = "0.3.55"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
[[package]]
name = "getrandom"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi",
]
[[package]] [[package]]
name = "hash_test" name = "hash_test"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"fasthash", "fasthash",
"hashbrown", "hashbrown",
"rand 0.8.5",
] ]
[[package]] [[package]]
@ -110,6 +122,12 @@ version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.4.6" version = "0.4.6"
@ -123,6 +141,27 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core 0.6.4",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core 0.6.4",
]
[[package]] [[package]]
name = "rand_core" name = "rand_core"
version = "0.3.1" version = "0.3.1"
@ -138,6 +177,15 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "rdrand" name = "rdrand"
version = "0.4.0" version = "0.4.0"
@ -159,6 +207,12 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"
@ -187,5 +241,5 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0eeda34baec49c4f1eb2c04d59b761582fd6330010f9330ca696ca1a355dfcd" checksum = "e0eeda34baec49c4f1eb2c04d59b761582fd6330010f9330ca696ca1a355dfcd"
dependencies = [ dependencies = [
"rand", "rand 0.4.6",
] ]

View File

@ -9,6 +9,7 @@ edition = "2018"
[dependencies] [dependencies]
hashbrown = "0.14" hashbrown = "0.14"
fasthash = "0.4.0" fasthash = "0.4.0"
rand = "0.8.5"
[profile.release] [profile.release]
opt-level = 3 opt-level = 3

View File

@ -1,19 +1,23 @@
use fasthash::spooky; use fasthash::spooky;
use hashbrown::HashSet; use hashbrown::HashSet;
use std::fs::File; use rand::prelude::*;
use std::io::{BufReader, BufRead};
use std::time::Instant; use std::time::Instant;
const CAP: i32 = 1000000;
fn main() { fn main() {
let arg: Vec<String> = std::env::args().collect(); let mut v: Vec<i32> = Vec::with_capacity(CAP as usize);
if arg.len() != 2 { let mut rng = thread_rng();
eprintln!("{} numbers.txt", std::env::args().nth(0).unwrap());
std::process::exit(1); let mut i = 0i32;
loop {
if i == CAP {
break;
}
i += 1;
v.push(rng.gen_range(-CAP..CAP));
} }
let name = &arg[1];
let f = File::open(name).expect("file can't open");
let reader = BufReader::new(f);
let v: Vec<i32> = reader.lines().map(|x| x.unwrap().parse::<i32>().unwrap()).collect();
{ {
let mut h = HashSet::with_hasher(spooky::Hash64); let mut h = HashSet::with_hasher(spooky::Hash64);
@ -28,7 +32,6 @@ fn main() {
bench_std(&v, &mut h, true); bench_std(&v, &mut h, true);
bench_std(&v, &mut h, false); bench_std(&v, &mut h, false);
} }
} }
fn bench_swiss(v: &Vec<i32>, h: &mut HashSet<i32, spooky::Hash64>, ins: bool) { fn bench_swiss(v: &Vec<i32>, h: &mut HashSet<i32, spooky::Hash64>, ins: bool) {
@ -36,16 +39,20 @@ fn bench_swiss(v: &Vec<i32>, h: &mut HashSet<i32, spooky::Hash64>, ins: bool) {
if ins { if ins {
for i in v { for i in v {
h.insert(*i); h.insert(*i);
} }
} else { } else {
for i in v { for i in v {
h.contains(i); h.contains(i);
} }
} }
let elapsed = now.elapsed().as_millis(); let elapsed = now.elapsed().as_millis();
println!("swiss {} => {}ms", if ins { "insert" } else { "search" }, elapsed); println!(
"swiss {} => {}ms",
if ins { "insert" } else { "search" },
elapsed
);
} }
fn bench_std(v: &Vec<i32>, h: &mut std::collections::HashSet<i32, spooky::Hash64>, ins: bool) { fn bench_std(v: &Vec<i32>, h: &mut std::collections::HashSet<i32, spooky::Hash64>, ins: bool) {
@ -53,14 +60,18 @@ fn bench_std(v: &Vec<i32>, h: &mut std::collections::HashSet<i32, spooky::Hash64
if ins { if ins {
for i in v { for i in v {
h.insert(*i); h.insert(*i);
} }
} else { } else {
for i in v { for i in v {
h.contains(i); h.contains(i);
} }
} }
let elapsed = now.elapsed().as_millis(); let elapsed = now.elapsed().as_millis();
println!("std {} => {}ms", if ins { "insert" } else { "search" }, elapsed); println!(
"std {} => {}ms",
if ins { "insert" } else { "search" },
elapsed
);
} }