it runs!
This commit is contained in:
parent
edcc931402
commit
c35818f048
@ -6,7 +6,7 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# tokio = { version = "1.35.1", features = ["full"] }
|
# tokio = { version = "1.36.0", features = ["full"] }
|
||||||
|
|
||||||
# serde = { version = "1.0", features = ["derive"] }
|
# serde = { version = "1.0", features = ["derive"] }
|
||||||
# md-5 = "0.10.6"
|
# md-5 = "0.10.6"
|
||||||
@ -16,3 +16,7 @@ base16384 = "0.1.0"
|
|||||||
|
|
||||||
tracing = "0.1.40"
|
tracing = "0.1.40"
|
||||||
tracing-subscriber = { version = "0.3.18", features = ["time"] }
|
tracing-subscriber = { version = "0.3.18", features = ["time"] }
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
opt-level = 0
|
||||||
|
codegen-units = 1
|
||||||
|
99
src/main.rs
99
src/main.rs
@ -1,10 +1,8 @@
|
|||||||
mod data_struct;
|
mod data_struct;
|
||||||
mod name;
|
mod name;
|
||||||
|
|
||||||
use std::sync::{atomic::AtomicU32, Arc};
|
|
||||||
|
|
||||||
use base16384::Base16384Utf8;
|
use base16384::Base16384Utf8;
|
||||||
use tracing::{info, warn};
|
use tracing::info;
|
||||||
|
|
||||||
/// 根据 u64 生成对应的 name
|
/// 根据 u64 生成对应的 name
|
||||||
/// 转换成 base 16384
|
/// 转换成 base 16384
|
||||||
@ -36,15 +34,45 @@ pub fn show_name(namer: &name::Namer) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_upper_case_globals)]
|
#[allow(non_upper_case_globals)]
|
||||||
const allow_d: u32 = 100;
|
const allow_d: u32 = 10;
|
||||||
|
|
||||||
|
#[allow(non_upper_case_globals)]
|
||||||
|
const report_interval: u32 = 1_000_0000;
|
||||||
|
|
||||||
|
fn cacl(max: u64, step: usize, top: u32, id: u64) {
|
||||||
|
let start_time = std::time::Instant::now();
|
||||||
|
let mut k: u64 = 0;
|
||||||
|
let mut top = top;
|
||||||
|
for i in (0+id..max).step_by(step) {
|
||||||
|
// let name = gen_name(i as u64);
|
||||||
|
let full_name = format!("{}@shenjack", i);
|
||||||
|
let namer = name::Namer::new(&full_name);
|
||||||
|
if let Some(namer) = namer {
|
||||||
|
let prop = namer.get_property();
|
||||||
|
if (prop + allow_d as f32) > top as f32 {
|
||||||
|
if prop > top as f32 {
|
||||||
|
info!("新的最高属性 {}", prop);
|
||||||
|
top = prop as u32;
|
||||||
|
}
|
||||||
|
info!("{:>10}|{}|{}", i, full_name, show_name(&namer));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
k += 1;
|
||||||
|
if k > report_interval as u64 {
|
||||||
|
let now = std::time::Instant::now();
|
||||||
|
info!("{} {} {}/s", k, id, k / now.duration_since(start_time).as_secs());
|
||||||
|
k = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
let team = "shenjack";
|
// let team = "shenjack";
|
||||||
// cli 获取 max
|
// cli 获取 max
|
||||||
let max = match std::env::args().nth(1) {
|
let max = match std::env::args().nth(1) {
|
||||||
Some(arg) => arg.parse().unwrap_or(1_0000_0000),
|
Some(arg) => arg.parse().unwrap_or(i64::MAX),
|
||||||
None => 1_0000_0000,
|
None => i64::MAX,
|
||||||
};
|
};
|
||||||
// cli 获取线程数量
|
// cli 获取线程数量
|
||||||
let thread_count = match std::env::args().nth(2) {
|
let thread_count = match std::env::args().nth(2) {
|
||||||
@ -53,54 +81,25 @@ fn main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 新建线程计算
|
// 新建线程计算
|
||||||
let top = AtomicU32::new(100);
|
let top: u32 = 750;
|
||||||
let arc_top = Arc::new(top);
|
|
||||||
// 将数据量处理成可被 thread_count 整除
|
// 将数据量处理成可被 thread_count 整除
|
||||||
// 在主线程计算剩余的
|
|
||||||
let left = max % thread_count;
|
let left = max % thread_count;
|
||||||
|
// max += left;
|
||||||
|
|
||||||
let threads = (0..thread_count).map(|i| {
|
let mut n = 0;
|
||||||
let top = arc_top.clone();
|
let mut threads = Vec::with_capacity(thread_count as usize);
|
||||||
|
for i in 0..thread_count {
|
||||||
|
let top = top;
|
||||||
let max = max / thread_count + if i < left { 1 } else { 0 };
|
let max = max / thread_count + if i < left { 1 } else { 0 };
|
||||||
std::thread::spawn(move || {
|
n += 1;
|
||||||
for i in 0..max+1 {
|
let thread_name = format!("thread_{}", i);
|
||||||
let name = gen_name(i as u64);
|
threads.push(std::thread::spawn(move || {
|
||||||
let full_name = format!("{}@{}", name, team);
|
info!("线程 {} 开始计算", thread_name);
|
||||||
let namer = name::Namer::new(&full_name);
|
cacl(max as u64, thread_count as usize, top as u32, n as u64);
|
||||||
if let Some(namer) = namer {
|
info!("线程 {} 结束计算", thread_name);
|
||||||
let prop = namer.get_property();
|
}));
|
||||||
let tmp_top = top.as_ref();
|
|
||||||
let top = tmp_top.load(std::sync::atomic::Ordering::Relaxed);
|
|
||||||
if (prop - allow_d as f32) > top as f32 {
|
|
||||||
if prop > top as f32 {
|
|
||||||
warn!("新的最高属性 {}", top);
|
|
||||||
tmp_top.store(prop as u32, std::sync::atomic::Ordering::Relaxed);
|
|
||||||
}
|
|
||||||
info!("{}|{}", full_name, show_name(&namer));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});
|
|
||||||
info!("开始计算");
|
|
||||||
|
|
||||||
for i in max-left..max+1 {
|
|
||||||
let name = gen_name(i as u64);
|
|
||||||
let full_name = format!("{}@{}", name, team);
|
|
||||||
let namer = name::Namer::new(&full_name);
|
|
||||||
if let Some(namer) = namer {
|
|
||||||
let prop = namer.get_property();
|
|
||||||
let tmp_top = arc_top.as_ref();
|
|
||||||
let top = tmp_top.load(std::sync::atomic::Ordering::Relaxed);
|
|
||||||
if (prop - allow_d as f32) > top as f32 {
|
|
||||||
if prop > top as f32 {
|
|
||||||
warn!("新的最高属性 {}", top as f32);
|
|
||||||
tmp_top.store(prop as u32, std::sync::atomic::Ordering::Relaxed);
|
|
||||||
}
|
|
||||||
info!("{}|{}", full_name, show_name(&namer));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
info!("开始计算");
|
||||||
|
|
||||||
for t in threads {
|
for t in threads {
|
||||||
t.join().unwrap();
|
t.join().unwrap();
|
||||||
|
Loading…
Reference in New Issue
Block a user