speed it up by parallelizing the 20 iterations

This commit is contained in:
jacekpoz 2024-06-05 17:25:16 +02:00
parent 78a1b3bc5a
commit b341d9d1cb
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8

View file

@ -101,17 +101,17 @@ fn main() -> io::Result<()> {
if rand_heights_clone.get(&n).is_some() {
return;
}
println!("{}: starting n: {n}", current_thread_index().unwrap());
println!("{thread}: starting n: {n}", thread = current_thread_index().unwrap());
asc_results_clone.insert(n, vec![]);
asc_heights_clone.insert(n, vec![]);
rand_results_clone.insert(n, vec![]);
rand_heights_clone.insert(n, vec![]);
(1..=m).into_par_iter().for_each(|k| {
println!("{thread}: starting iteration {k}/{m} for n: {n}", thread = current_thread_index().unwrap());
let mut asc = BinarySearchTree::new(n as usize);
let mut rand = BinarySearchTree::new(n as usize);
for _ in 0..m {
for i in gen_asc(n) {
asc.insert(i);
asc_heights_clone.get_mut(&n).unwrap().push(asc.height());
@ -123,6 +123,8 @@ fn main() -> io::Result<()> {
asc_results_clone.get_mut(&n).unwrap().push(asc.stats.clone());
let mut rand = BinarySearchTree::new(n as usize);
for i in gen_rand(n) {
rand.insert(i);
rand_heights_clone.get_mut(&n).unwrap().push(rand.height());
@ -133,8 +135,9 @@ fn main() -> io::Result<()> {
}
rand_results_clone.get_mut(&n).unwrap().push(rand.stats.clone());
}
println!("{}: finished n: {n}", current_thread_index().unwrap());
println!("{thread}: finished iteration {k}/{m} for n: {n}", thread = current_thread_index().unwrap());
});
println!("{thread}: finished n: {n}", thread = current_thread_index().unwrap());
});
let asc_comp_averages = comp_avg(&asc_results);