killer addition works
Cargo Check, Format, Fix and Test / cargo CI (push) Successful in 1m30s
Details
Cargo Check, Format, Fix and Test / cargo CI (push) Successful in 1m30s
Details
This commit is contained in:
parent
13c19c1831
commit
bc7eb41675
|
@ -1,10 +1,42 @@
|
|||
use std::io::{prelude::*, Sink};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use rayon::prelude::*;
|
||||
use tokio::time::{interval, Instant};
|
||||
|
||||
// if we make these larger, our computer can be used as a heater🔥
|
||||
type Danum = u16;
|
||||
const CAP: usize = 1 << 14;
|
||||
const M: u128 = CAP as u128 * Danum::MAX as u128;
|
||||
|
||||
fn status(start: &Instant, range: &Vec<Danum>) -> bool {
|
||||
let sum: u128 = { range.par_iter().map(|n| *n as u128).sum::<u128>() };
|
||||
if sum < 1 {
|
||||
return false;
|
||||
}
|
||||
let progress = sum as f64 / M as f64;
|
||||
let eq = sum == M;
|
||||
println!(
|
||||
r#"
|
||||
done: {}
|
||||
current threads: {}
|
||||
progress: {}%
|
||||
log_2(capacity): {}
|
||||
log_2(sum): {}
|
||||
cap: {}
|
||||
sum: {}
|
||||
took: {:?}
|
||||
"#,
|
||||
eq,
|
||||
rayon::current_num_threads(),
|
||||
progress * 100.0,
|
||||
CAP.ilog2(),
|
||||
sum.ilog2(),
|
||||
CAP,
|
||||
sum,
|
||||
start.elapsed(),
|
||||
);
|
||||
eq
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
@ -14,22 +46,23 @@ async fn main() {
|
|||
unsafe {
|
||||
range.set_len(range.capacity());
|
||||
}
|
||||
let now = std::time::Instant::now();
|
||||
range.par_iter_mut().for_each(|num| {
|
||||
let mut sink = Sink::default();
|
||||
while *num < Danum::MAX {
|
||||
*num += 1; // cannot use `+=` on type `&u8`
|
||||
let _ = write!(sink, "{num}"); // just to disable the compiler from calculating it all
|
||||
// beforehand
|
||||
|
||||
let start = Instant::now();
|
||||
let lock = Arc::new(Mutex::new(range));
|
||||
let lock2 = lock.clone();
|
||||
rayon::spawn(move || {
|
||||
for n in 0..CAP {
|
||||
let mut range = lock.lock().unwrap();
|
||||
for _ in 0..Danum::MAX {
|
||||
range[n] += 1;
|
||||
}
|
||||
}
|
||||
});
|
||||
let sum: u128 = { range.par_iter().map(|n| *n as u128).sum::<u128>() };
|
||||
let eq = sum == CAP as u128 * Danum::MAX as u128;
|
||||
println!(
|
||||
"log cap: {}\nit worked: {eq}\nsum: {sum}\nlog_2(sum): {}\ntook: {:?}\nused threads: {}",
|
||||
CAP.ilog2(),
|
||||
sum.ilog2(),
|
||||
now.elapsed(),
|
||||
rayon::current_num_threads()
|
||||
);
|
||||
let mut ticker = interval(tokio::time::Duration::from_millis(500));
|
||||
loop {
|
||||
ticker.tick().await;
|
||||
if status(&start, &lock2.lock().unwrap()) {
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue