rayon example for wasting cpu time :)
Cargo Check, Format, Fix and Test / cargo CI (push) Successful in 1m31s
Details
Cargo Check, Format, Fix and Test / cargo CI (push) Successful in 1m31s
Details
This commit is contained in:
parent
7c5df54dff
commit
13c19c1831
|
@ -1,43 +1,35 @@
|
||||||
use std::sync::atomic::{AtomicU32, Ordering};
|
use std::io::{prelude::*, Sink};
|
||||||
|
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use tokio::{
|
|
||||||
self,
|
|
||||||
time::{interval, Interval},
|
|
||||||
};
|
|
||||||
|
|
||||||
static THE_NUMBER: AtomicU32 = AtomicU32::new(0);
|
// if we make these larger, our computer can be used as a heater🔥
|
||||||
|
type Danum = u16;
|
||||||
#[inline]
|
const CAP: usize = 1 << 14;
|
||||||
fn incr() {
|
|
||||||
// just add it
|
|
||||||
THE_NUMBER.store(THE_NUMBER.load(Ordering::Relaxed) + 1, Ordering::Relaxed)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn get() -> u32 {
|
|
||||||
THE_NUMBER.load(Ordering::Relaxed)
|
|
||||||
}
|
|
||||||
#[inline]
|
|
||||||
async fn more() {
|
|
||||||
rayon::spawn(|| incr())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// Lets say that we want to add numbers FAST
|
// Lets say that we want to add many numbers FAST
|
||||||
println!("The number: {THE_NUMBER:?}");
|
let mut range: Vec<Danum> = Vec::with_capacity(CAP);
|
||||||
incr();
|
// Initialize the values, probably zero
|
||||||
assert_eq!(get(), 1);
|
unsafe {
|
||||||
println!("The number: {THE_NUMBER:?}");
|
range.set_len(range.capacity());
|
||||||
println!("starting the threads");
|
|
||||||
let mut interval = interval(tokio::time::Duration::from_millis(100));
|
|
||||||
loop {
|
|
||||||
tokio::select! {
|
|
||||||
_ = interval.tick() => {
|
|
||||||
println!("The number: {THE_NUMBER:?}");
|
|
||||||
}
|
|
||||||
_ = more() => ()
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
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 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()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue