From af5ea9445c5eae2650867d0f1f650f0de6b142ab Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 23 Aug 2023 09:29:45 +1000 Subject: [PATCH] Use `into_iter` instead of `iter`. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 ``` --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7304db3..a54bcde 100644 --- a/src/main.rs +++ b/src/main.rs @@ -158,7 +158,7 @@ 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() }); @@ -166,7 +166,7 @@ where 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!(