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"
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]]
name = "hash_test"
version = "0.1.0"
dependencies = [
"fasthash",
"hashbrown",
"rand 0.8.5",
]
[[package]]
@ -110,6 +122,12 @@ version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "rand"
version = "0.4.6"
@ -123,6 +141,27 @@ dependencies = [
"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]]
name = "rand_core"
version = "0.3.1"
@ -138,6 +177,15 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "rdrand"
version = "0.4.0"
@ -159,6 +207,12 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "winapi"
version = "0.3.9"
@ -187,5 +241,5 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0eeda34baec49c4f1eb2c04d59b761582fd6330010f9330ca696ca1a355dfcd"
dependencies = [
"rand",
"rand 0.4.6",
]

View File

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

View File

@ -1,19 +1,23 @@
use fasthash::spooky;
use hashbrown::HashSet;
use std::fs::File;
use std::io::{BufReader, BufRead};
use rand::prelude::*;
use std::time::Instant;
const CAP: i32 = 1000000;
fn main() {
let arg: Vec<String> = std::env::args().collect();
if arg.len() != 2 {
eprintln!("{} numbers.txt", std::env::args().nth(0).unwrap());
std::process::exit(1);
let mut v: Vec<i32> = Vec::with_capacity(CAP as usize);
let mut rng = thread_rng();
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);
@ -28,7 +32,6 @@ fn main() {
bench_std(&v, &mut h, true);
bench_std(&v, &mut h, false);
}
}
fn bench_swiss(v: &Vec<i32>, h: &mut HashSet<i32, spooky::Hash64>, ins: bool) {
@ -36,7 +39,7 @@ fn bench_swiss(v: &Vec<i32>, h: &mut HashSet<i32, spooky::Hash64>, ins: bool) {
if ins {
for i in v {
h.insert(*i);
h.insert(*i);
}
} else {
for i in v {
@ -45,7 +48,11 @@ fn bench_swiss(v: &Vec<i32>, h: &mut HashSet<i32, spooky::Hash64>, ins: bool) {
}
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) {
@ -53,7 +60,7 @@ fn bench_std(v: &Vec<i32>, h: &mut std::collections::HashSet<i32, spooky::Hash64
if ins {
for i in v {
h.insert(*i);
h.insert(*i);
}
} else {
for i in v {
@ -62,5 +69,9 @@ fn bench_std(v: &Vec<i32>, h: &mut std::collections::HashSet<i32, spooky::Hash64
}
let elapsed = now.elapsed().as_millis();
println!("std {} => {}ms", if ins { "insert" } else { "search" }, elapsed);
println!(
"std {} => {}ms",
if ins { "insert" } else { "search" },
elapsed
);
}