forked from chapel-lang/chapel
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rust sorting benchmark (chapel-lang#24302)
[PR by @mo8it - thanks! reviewed/merged by @mppf] This PR adjusts the Rust comparison in `test/library/packages/Sort/performance/comparison/` and adds a Rust+Rayon version.
- Loading branch information
Showing
7 changed files
with
98 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ tags | |
.idea/ | ||
cmake-build-*/ | ||
.cache/ | ||
target/ | ||
Cargo.lock | ||
|
||
# Top level ignores. | ||
/BUILD_VERSION | ||
|
5 changes: 5 additions & 0 deletions
5
test/library/packages/Sort/performance/comparison/sort-random-rust-rayon/.cargo/config.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[build] | ||
rustflags = [ | ||
"-C", | ||
"target-cpu=native", | ||
] |
15 changes: 15 additions & 0 deletions
15
test/library/packages/Sort/performance/comparison/sort-random-rust-rayon/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "sort-random-rust-rayon" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
rand = { version = "0.8.4", features = ["small_rng"] } | ||
rayon = "1.8.1" | ||
|
||
[profile.release] | ||
lto = true | ||
codegen-units = 1 | ||
strip = true | ||
panic = "abort" |
38 changes: 38 additions & 0 deletions
38
test/library/packages/Sort/performance/comparison/sort-random-rust-rayon/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// run it with | ||
// cd sort-random-rust | ||
// cargo run --release | ||
|
||
use rand::{distributions::Standard, rngs::SmallRng, Rng, SeedableRng}; | ||
use rayon::prelude::*; | ||
use std::time::Instant; | ||
|
||
fn main() { | ||
let n = 128 * 1024 * 1024; | ||
|
||
let t1 = Instant::now(); | ||
let rng = SmallRng::seed_from_u64(42); | ||
let mut values = Vec::<u64>::new(); | ||
(0..n) | ||
.into_par_iter() | ||
.map_with(rng, |rng, _| rng.sample(Standard)) | ||
.collect_into_vec(&mut values); | ||
let gen_duration = t1.elapsed(); | ||
println!( | ||
"generating {} MiB/s", | ||
8.0 * (n as f64) / gen_duration.as_secs_f64() / 1024.0 / 1024.0 | ||
); | ||
|
||
let start = Instant::now(); | ||
values.par_sort_unstable(); | ||
let duration = start.elapsed(); | ||
|
||
println!( | ||
"{} MiB/s", | ||
8.0 * (n as f64) / duration.as_secs_f64() / 1024.0 / 1024.0 | ||
); | ||
|
||
println!( | ||
"{} million elements sorted per second", | ||
(n as f64) / duration.as_secs_f64() / 1000.0 / 1000.0 | ||
); | ||
} |
5 changes: 5 additions & 0 deletions
5
test/library/packages/Sort/performance/comparison/sort-random-rust/.cargo/config.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[build] | ||
rustflags = [ | ||
"-C", | ||
"target-cpu=native", | ||
] |
13 changes: 9 additions & 4 deletions
13
test/library/packages/Sort/performance/comparison/sort-random-rust/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
[package] | ||
name = "sort-random-rust" | ||
version = "0.1.0" | ||
version = "0.0.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
publish = false | ||
|
||
[dependencies] | ||
rand = "0.8.4" | ||
rand = { version = "0.8.4", features = ["small_rng"] } | ||
rayon = "1.8.1" | ||
|
||
[profile.release] | ||
lto = true | ||
codegen-units = 1 | ||
strip = true | ||
panic = "abort" |
34 changes: 24 additions & 10 deletions
34
test/library/packages/Sort/performance/comparison/sort-random-rust/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,39 @@ | ||
// run it with | ||
// cd sort-random-rust | ||
// cargo build --release | ||
// target/release/sort-random-rust | ||
use rand::prelude::*; | ||
use rand::distributions::Standard; | ||
// cargo run --release | ||
|
||
use rand::{distributions::Standard, rngs::SmallRng, Rng, SeedableRng}; | ||
use rayon::prelude::*; | ||
use std::time::Instant; | ||
|
||
fn main() { | ||
let n = 128*1024*1024; | ||
let n = 128 * 1024 * 1024; | ||
|
||
let t1 = Instant::now(); | ||
let mut values: Vec<u64> = rand::thread_rng().sample_iter(Standard).take(n).collect(); | ||
let rng = SmallRng::seed_from_u64(42); | ||
let mut values = Vec::<u64>::new(); | ||
(0..n) | ||
.into_par_iter() | ||
.map_with(rng, |rng, _| rng.sample(Standard)) | ||
.collect_into_vec(&mut values); | ||
let gen_duration = t1.elapsed(); | ||
println!("generating {:?} MiB/s", 8.0*(n as f64)/gen_duration.as_secs_f64()/1024.0/1024.0); | ||
println!( | ||
"generating {} MiB/s", | ||
8.0 * (n as f64) / gen_duration.as_secs_f64() / 1024.0 / 1024.0 | ||
); | ||
|
||
let start = Instant::now(); | ||
// Without rayon. | ||
values.sort_unstable(); | ||
let duration = start.elapsed(); | ||
|
||
println!("{:?} MiB/s", 8.0*(n as f64)/duration.as_secs_f64()/1024.0/1024.0); | ||
println!( | ||
"{} MiB/s", | ||
8.0 * (n as f64) / duration.as_secs_f64() / 1024.0 / 1024.0 | ||
); | ||
|
||
println!("{:?} million elements sorted per second", | ||
(n as f64)/duration.as_secs_f64()/1000.0/1000.0); | ||
println!( | ||
"{} million elements sorted per second", | ||
(n as f64) / duration.as_secs_f64() / 1000.0 / 1000.0 | ||
); | ||
} |