From b88fdd7665e020296e44bea56500aac0cab0cd91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Wed, 30 Oct 2024 22:45:12 +0100 Subject: [PATCH] Add the rayon_sorted_reverse_2 algorithm --- README.md | 2 ++ benches/job_scheduling.rs | 1 + src/algorithm.rs | 2 +- src/algorithm/rayon.rs | 6 ++++++ 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a375430..c323832 100644 --- a/README.md +++ b/README.md @@ -72,5 +72,7 @@ algorithms in Rust and benchmark them. Let the battle begin. - `rayon sorted reverse`: Multi-threading provided by the `rayon` crate. Tasks are sorted so the longest ones are executed first. This should behave as an [LPT][lpt]. +- `rayon sorted reverse`: Similar to `rayon sorted reverse`, but tasks are + sorted from shortest to longest and are then iterated in the reverse order. [lpt]: https://en.wikipedia.org/wiki/Longest-processing-time-first_scheduling diff --git a/benches/job_scheduling.rs b/benches/job_scheduling.rs index 37184aa..8fb62c1 100644 --- a/benches/job_scheduling.rs +++ b/benches/job_scheduling.rs @@ -9,6 +9,7 @@ const TEST_FUNCS: &[(&str, &dyn Fn(&[Task]))] = &[ ("rayon", &rayon), ("rayon sorted", &rayon_sorted), ("rayon sorted reverse", &rayon_sorted_reverse), + ("rayon sorted reverse 2", &rayon_sorted_reverse_2), ]; fn job_scheduling_benchmark(c: &mut Criterion) { diff --git a/src/algorithm.rs b/src/algorithm.rs index 52541ff..f75d047 100644 --- a/src/algorithm.rs +++ b/src/algorithm.rs @@ -1,5 +1,5 @@ mod rayon; mod single_thread; -pub use rayon::{rayon, rayon_sorted, rayon_sorted_reverse}; +pub use rayon::*; pub use single_thread::single_thread; diff --git a/src/algorithm/rayon.rs b/src/algorithm/rayon.rs index da5ce73..fdac951 100644 --- a/src/algorithm/rayon.rs +++ b/src/algorithm/rayon.rs @@ -18,3 +18,9 @@ pub fn rayon_sorted_reverse(data: &[Task]) { data.sort_by(|a, b| a.cmp(b).reverse()); rayon(&data); } + +pub fn rayon_sorted_reverse_2(data: &[Task]) { + let mut data = data.to_vec(); + data.sort(); + data.into_par_iter().rev().for_each(|task| task.execute()); +}