From d8f34487dc1eca3af07c4e31b14b55572339e9cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Wed, 30 Oct 2024 21:21:40 +0100 Subject: [PATCH] Add the rayon and rayon_sorted algorithms --- Cargo.toml | 1 + benches/job_scheduling.rs | 6 +++++- src/algorithm.rs | 2 ++ src/algorithm/rayon.rs | 12 ++++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/algorithm/rayon.rs diff --git a/Cargo.toml b/Cargo.toml index dafe03c..2c5c58f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ publish = false [dependencies] pbkdf2 = "0.12.2" +rayon = { version = "1.10.0", default-features = false } sha2 = "0.10.8" [dev-dependencies] diff --git a/benches/job_scheduling.rs b/benches/job_scheduling.rs index f7f68b8..3ac854d 100644 --- a/benches/job_scheduling.rs +++ b/benches/job_scheduling.rs @@ -4,7 +4,11 @@ use rust_job_scheduling::data::SMALL; use rust_job_scheduling::Task; const TEST_DATA: &[(&str, &[Task])] = &[("small", SMALL)]; -const TEST_FUNCS: &[(&str, &dyn Fn(&[Task]))] = &[("single thread", &single_thread)]; +const TEST_FUNCS: &[(&str, &dyn Fn(&[Task]))] = &[ + ("single thread", &single_thread), + ("rayon", &rayon), + ("rayon sorted", &rayon_sorted), +]; fn job_scheduling_benchmark(c: &mut Criterion) { let mut group = c.benchmark_group("job_scheduling"); diff --git a/src/algorithm.rs b/src/algorithm.rs index 0ebf2b7..7075acb 100644 --- a/src/algorithm.rs +++ b/src/algorithm.rs @@ -1,3 +1,5 @@ +mod rayon; mod single_thread; +pub use rayon::{rayon, rayon_sorted}; pub use single_thread::single_thread; diff --git a/src/algorithm/rayon.rs b/src/algorithm/rayon.rs new file mode 100644 index 0000000..0fad83b --- /dev/null +++ b/src/algorithm/rayon.rs @@ -0,0 +1,12 @@ +use crate::Task; +use rayon::prelude::*; + +pub fn rayon(data: &[Task]) { + data.into_par_iter().for_each(|task| task.execute()); +} + +pub fn rayon_sorted(data: &[Task]) { + let mut data = data.to_vec(); + data.sort(); + rayon(&data) +}