fn cpy_task(chnk: Vec<String>, path_to: String) {
let start = std::time::Instant::now();
for (current_inx, current_file) in chnk.iter().enumerate() {
let current_file_name = PathBuf::from(current_file.clone());
let current_file_name = current_file_name.file_name().unwrap().to_str().unwrap();
let full_path_to = path_to.clone() + "/" + current_file_name;
std::fs::copy(current_file,full_path_to);
}
let duration = start.elapsed();
println!("Copying finished:{}", duration.as_secs());
}
This is my parallel loop (assume that the number of threads is never more than number of processors - 1)
#[cfg(cpy_parallel)]
{
let start_parallel = std::time::Instant::now();
let mut handles: Vec<std::thread::JoinHandle<()>> = Vec::new();
for i in paths_from {
let path_to_clone = path_to.clone();
let handle: std::thread::JoinHandle<()> = std::thread::spawn(move || {
cpy_task(vec![i.clone()], path_to_clone);
});
handles.push(handle);
}
for handle in handles {
handle.join();
}
let duration_parallel = start_parallel.elapsed();
println!("Copying in parallel finished:{}", duration_parallel.as_secs());
}
This above is slower or executes with similar speed to the sequential counter part:
cpy_task(paths_from, path_to);
Would anyone have idea why is it behaving in such counter intuitive way?
Thanks