From 3377430769e4687f888c2e65005877b385359bb1 Mon Sep 17 00:00:00 2001 From: Brian Hicks Date: Tue, 24 Aug 2021 17:15:44 -0500 Subject: [PATCH] calculate sizes in parallel --- BENCHMARKING.md | 22 ++++++++++++++++++++++ src/main.rs | 14 +++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/BENCHMARKING.md b/BENCHMARKING.md index 033ead6..c29baa5 100644 --- a/BENCHMARKING.md +++ b/BENCHMARKING.md @@ -148,3 +148,25 @@ Summary './target/release/similar-sort benchmark < /usr/share/dict/words' ran 1.74 ± 0.08 times faster than './result/bin/similar-sort define < /usr/share/dict/words' ``` + +## Calculating sizes in parallel + +What if we calculated the size in parallel? +Could we get it even faster? + +``` +hyperfine './result/bin/similar-sort define < /usr/share/dict/words' './target/release/similar-sort benchmark < /usr/share/dict/words' +Benchmark #1: ./result/bin/similar-sort define < /usr/share/dict/words + Time (mean ± σ): 295.0 ms ± 5.6 ms [User: 259.3 ms, System: 76.5 ms] + Range (min … max): 287.4 ms … 305.2 ms 10 runs + +Benchmark #2: ./target/release/similar-sort benchmark < /usr/share/dict/words + Time (mean ± σ): 153.5 ms ± 3.4 ms [User: 143.0 ms, System: 11.0 ms] + Range (min … max): 147.5 ms … 163.0 ms 19 runs + +Summary + './target/release/similar-sort benchmark < /usr/share/dict/words' ran + 1.92 ± 0.06 times faster than './result/bin/similar-sort define < /usr/share/dict/words' +``` + +Yep! diff --git a/src/main.rs b/src/main.rs index f57d537..6490164 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,17 +25,21 @@ fn try_main() -> Result<()> { let opts = Opts::from_args(); - let mut lines: Vec<(usize, String)> = stdin() + let lines: Vec = stdin() .lock() .lines() - .map(|line| line.map(|candidate| (levenshtein(&opts.target, &candidate), candidate))) - .collect::>>() + .collect::>>() .context("could not read lines from stdin")?; - lines.par_sort_unstable_by_key(|x| x.0); + let mut distances: Vec<(usize, &String)> = lines + .iter() + .map(|candidate| (levenshtein(&opts.target, candidate), candidate)) + .collect(); + + distances.par_sort_unstable_by_key(|x| x.0); let mut out = BufWriter::new(stdout()); - for (_, candidate) in lines { + for (_, candidate) in distances { writeln!(out, "{}", candidate).context("could not write to stdout")?; }