Skip to content

Commit

Permalink
Add a parallel version of the previous example
Browse files Browse the repository at this point in the history
  • Loading branch information
Jed authored and Chris00 committed Dec 27, 2023
1 parent de26e0a commit ce43caa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ nightly = []
criterion = "0.3.5"
roots = "0.0.7"
rand = "0.8.5"
rayon = "1.8.0"

[[example]]
name = "basic"
Expand Down
29 changes: 29 additions & 0 deletions examples/x_sinx_levels_par.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Parallel version of `x_sinx_levels.rs`.
use std::{error::Error, iter, time::Instant};
use root1d::{bisect, toms748};
use rand::prelude::*;
use rayon::prelude::*;

fn main() -> Result<(), Box<dyn Error + 'static>> {
const N: usize = 100_000;
let levels: Vec<_> = iter::from_fn(|| Some(1.5 * random::<f64>()))
.take(N).collect();
let mut out = vec![0.0; N];

let now = Instant::now();
levels
.par_iter()
.map(|lv| bisect(|x: f64| x * x.sin() - lv, 0., 2.).root().unwrap())
.collect_into_vec(&mut out);
println!("bisect: {} secs", now.elapsed().as_secs_f64());

let now = Instant::now();
levels
.par_iter()
.map(|lv| toms748(|x: f64| x * x.sin() - lv, 0., 2.).root().unwrap())
.collect_into_vec(&mut out);
println!("toms748: {} secs", now.elapsed().as_secs_f64());

Ok(())
}

0 comments on commit ce43caa

Please sign in to comment.