Skip to content

Commit 2921101

Browse files
author
Mark Lodato
committed
Add benchmarks for chain/flat_map. Needs macro
1 parent b6ddb29 commit 2921101

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

library/core/benches/iter.rs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ fn bench_lt(b: &mut Bencher) {
347347
}
348348

349349
#[bench]
350-
fn bench_fold_vec(b: &mut Bencher) {
350+
fn bench_fold_fold_mut_vec(b: &mut Bencher) {
351351
b.iter(|| {
352352
(0..100000).map(black_box).fold(Vec::new(), |mut v, n| {
353353
if n % 2 == 0 {
@@ -359,7 +359,7 @@ fn bench_fold_vec(b: &mut Bencher) {
359359
}
360360

361361
#[bench]
362-
fn bench_fold_mut_vec(b: &mut Bencher) {
362+
fn bench_fold_fold_mut_vec_mut(b: &mut Bencher) {
363363
b.iter(|| {
364364
(0..100000).map(black_box).fold_mut(Vec::new(), |v, n| {
365365
if n % 2 == 0 {
@@ -370,7 +370,7 @@ fn bench_fold_mut_vec(b: &mut Bencher) {
370370
}
371371

372372
#[bench]
373-
fn bench_fold_map(b: &mut Bencher) {
373+
fn bench_fold_fold_mut_hashmap(b: &mut Bencher) {
374374
use std::collections::HashMap;
375375

376376
b.iter(|| {
@@ -382,7 +382,7 @@ fn bench_fold_map(b: &mut Bencher) {
382382
}
383383

384384
#[bench]
385-
fn bench_fold_mut_map(b: &mut Bencher) {
385+
fn bench_fold_fold_mut_hashmap_mut(b: &mut Bencher) {
386386
use std::collections::HashMap;
387387

388388
b.iter(|| {
@@ -393,15 +393,48 @@ fn bench_fold_mut_map(b: &mut Bencher) {
393393
}
394394

395395
#[bench]
396-
fn bench_fold_num(b: &mut Bencher) {
396+
fn bench_fold_fold_mut_num(b: &mut Bencher) {
397397
b.iter(|| (0..100000).map(black_box).fold(0, |sum, n| sum + n));
398398
}
399399

400400
#[bench]
401-
fn bench_fold_mut_num(b: &mut Bencher) {
401+
fn bench_fold_fold_mut_num_mut(b: &mut Bencher) {
402402
b.iter(|| {
403403
(0..100000).map(black_box).fold_mut(0, |sum, n| {
404404
*sum += n;
405405
})
406406
});
407407
}
408+
409+
#[bench]
410+
fn bench_fold_fold_mut_chain(b: &mut Bencher) {
411+
b.iter(|| (0i64..1000000).chain(0..1000000).map(black_box).fold(0, |sum, n| sum + n));
412+
}
413+
414+
#[bench]
415+
fn bench_fold_fold_mut_chain_mut(b: &mut Bencher) {
416+
b.iter(|| {
417+
(0i64..1000000).chain(0..1000000).map(black_box).fold_mut(0, |sum, n| {
418+
*sum += n;
419+
})
420+
});
421+
}
422+
423+
#[bench]
424+
fn bench_fold_fold_mut_chain_flat_map(b: &mut Bencher) {
425+
b.iter(|| {
426+
(0i64..1000000)
427+
.flat_map(|x| once(x).chain(once(x)))
428+
.map(black_box)
429+
.fold(0, |sum, n| sum + n)
430+
});
431+
}
432+
433+
#[bench]
434+
fn bench_fold_fold_mut_chain_flat_map_mut(b: &mut Bencher) {
435+
b.iter(|| {
436+
(0i64..1000000).flat_map(|x| once(x).chain(once(x))).map(black_box).fold_mut(0, |sum, n| {
437+
*sum += n;
438+
})
439+
});
440+
}

0 commit comments

Comments
 (0)