I'm trying to parallelize a portion of my code, and despite it using rayon
and the parallel iterators par_iter()
and par_extend()
, it still looks like it runs on a single thread.
I simply create a vector of i32
, fill it up with a lot of values, and then move these values into a collections::HashSet
of integers.
My single threaded code:
use std::collections::HashSet;
fn main() {
let my_vec: Vec<i64> = (0..100_000_000).collect();
let mut my_set: HashSet<i64> = HashSet::new();
let st = std::time::Instant::now();
my_set.extend(
my_vec.iter().map(|x| x*(x+3)/77+44741) // this is supposed to take a while to compute
);
let dur = st.elapsed();
println!("{:?}", dur);
}
Running time is around 8.86 s
in average.
Here is the code using parallel iterators:
extern crate rayon;
use rayon::prelude::*;
use std::collections::HashSet;
fn main() {
let my_vec: Vec<i64> = (0..100_000_000).collect();
let mut my_set: HashSet<i64> = HashSet::new();
let st = std::time::Instant::now();
my_set.par_extend(
my_vec.par_iter().map(|x| x*(x+3)/77+44741) // this is supposed to take a while to compute
);
let dur = st.elapsed();
println!("{:?}", dur);
}
The average running time for the 'parallel' version is almost the same (8.62 s
), and the cpu monitor clearly shows that a single cpu is working at 100% while the others just wait.
Do you know what I did wrong, or did not understand?