Skip to content

Commit

Permalink
Use into_iter instead of iter.
Browse files Browse the repository at this point in the history
It's slightly faster. On a huge file, old:
```
Benchmark 1: target/release/counts big2 > /dev/null
  Time (mean ± σ):     942.4 ms ±  21.1 ms    [User: 884.6 ms, System: 57.7 ms]
  Range (min … max):   917.7 ms … 971.3 ms    10 runs
```
and new:
```
Benchmark 1: target/release/counts big2 > /dev/null
  Time (mean ± σ):     932.6 ms ±  14.2 ms    [User: 874.2 ms, System: 58.4 ms]
  Range (min … max):   902.8 ms … 952.0 ms    10 runs
```
  • Loading branch information
nnethercote committed Aug 22, 2023
1 parent 34ddb11 commit af5ea94
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ where

// Sort from highest count to lowest count. For lines with the same count,
// sort them in alphabetical order.
let mut counts: Vec<_> = counts.iter().collect();
let mut counts: Vec<_> = counts.into_iter().collect();
counts.sort_unstable_by(|(line1, n1), (line2, n2)| {
(n2.abs(), line1).partial_cmp(&(n1.abs(), line2)).unwrap()
});

writeln!(io::stdout(), "{:.1} counts{}", total, label)?;
let mut cum_perc: f64 = 0f64;
let total_f64 = total.into_f64();
for (i, (line, &weight)) in counts.iter().enumerate() {
for (i, (line, weight)) in counts.iter().enumerate() {
let perc: f64 = weight.into_f64() * 100f64 / total_f64;
cum_perc += perc;
writeln!(
Expand Down

0 comments on commit af5ea94

Please sign in to comment.