Skip to content

Commit

Permalink
Rust sorting benchmark (chapel-lang#24302)
Browse files Browse the repository at this point in the history
[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
mppf authored Feb 5, 2024
2 parents 3716a34 + 26e000f commit 6980ca9
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ tags
.idea/
cmake-build-*/
.cache/
target/
Cargo.lock

# Top level ignores.
/BUILD_VERSION
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[build]
rustflags = [
"-C",
"target-cpu=native",
]
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"
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
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[build]
rustflags = [
"-C",
"target-cpu=native",
]
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"
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
);
}

0 comments on commit 6980ca9

Please sign in to comment.