calculate sizes in parallel
parent
f0155566e1
commit
3377430769
|
@ -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
src/main.rs
14
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<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")?;
|
||||
}
|
||||
|
||||
|
|
Reference in New Issue