When I use these different solutions on leetcode the “for loop” version is 4ms and the “iterator” version is 44ms fairly consistently. I know the pseudo-benchmark I’m using on the playground is not equivalent, but it consistently seems to give a faster result with the “iterator” version. Can anyone explain why one might be faster or slower than the other?

```
use std::collections::HashMap;
fn main() {
let nums = vec![1, 2, 3, 4, 5];
let k = 1i32;
let start1 = time::precise_time_s();
for _ in 0..100000 {
find_pairs(nums.clone(), k);
}
let end1 = time::precise_time_s();
for _ in 0..100000 {
iter_find_pairs(nums.clone(), k);
}
let end2 = time::precise_time_s();
println!("for loop time: {}", end1 - start1);
println!("iterator approach time: {}", end2 - end1);
}
pub fn find_pairs(nums: Vec<i32>, k: i32) -> i32 {
if nums.is_empty() || k < 0 {
return 0;
}
let mut tally = HashMap::with_capacity(nums.len());
for num in nums {
*tally.entry(num).or_insert(0) += 1;
}
let mut count = 0;
for (num, _val) in tally.iter() {
if let Some(paired_num) = tally.get(&(num - k)) {
if k > 0 {
count += 1
} else if k == 0 && *paired_num > 1 {
count += 1
}
}
}
count
}
pub fn iter_find_pairs(nums: Vec<i32>, k: i32) -> i32 {
let mut tally = HashMap::with_capacity(nums.len());
for num in nums.iter() {
*tally.entry(num).or_insert(0) += 1;
}
match k {
k if k < 0 => 0,
k if k == 0 => tally.iter().filter(|(_, val)| **val > 1).count() as i32,
_ => tally
.iter()
.filter(|(x, _)| nums.contains(&(**x - k)))
.count() as i32,
}
}
```

Output:

```
for loop time: 1.5166003638878465
iterator approach time: 1.0781516963616014
```

Errors:

```
Compiling playground v0.0.1 (/playground)
Finished dev [unoptimized + debuginfo] target(s) in 1.02s
Running `target/debug/playground`
```