Skip to content

Commit

Permalink
calculate sizes in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Aug 24, 2021
1 parent f015556 commit 3377430
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
22 changes: 22 additions & 0 deletions BENCHMARKING.md
Expand Up @@ -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!
14 changes: 9 additions & 5 deletions src/main.rs
Expand Up @@ -25,17 +25,21 @@ fn try_main() -> Result<()> {

let opts = Opts::from_args();

let mut lines: Vec<(usize, String)> = stdin()
let lines: Vec<String> = stdin()
.lock()
.lines()
.map(|line| line.map(|candidate| (levenshtein(&opts.target, &candidate), candidate)))
.collect::<io::Result<Vec<(usize, String)>>>()
.collect::<io::Result<Vec<String>>>()
.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")?;
}

Expand Down

0 comments on commit 3377430

Please sign in to comment.