Add the rayon_sorted_reverse_2 algorithm

This commit is contained in:
Rodolphe Bréard 2024-10-30 22:45:12 +01:00
parent c6dacfcf22
commit b88fdd7665
4 changed files with 10 additions and 1 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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;

View file

@ -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());
}