Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialize StepBy<Range<{integer}>> #111850

Merged
merged 5 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions library/core/benches/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::borrow::Borrow;
use core::iter::*;
use core::mem;
use core::num::Wrapping;
use core::ops::Range;
use test::{black_box, Bencher};

#[bench]
Expand Down Expand Up @@ -69,6 +70,57 @@ fn bench_max(b: &mut Bencher) {
})
}

#[bench]
fn bench_range_step_by_sum_reducible(b: &mut Bencher) {
let r = 0u32..1024;
b.iter(|| {
let r = black_box(r.clone()).step_by(8);

let mut sum: u32 = 0;
for i in r {
sum += i;
}

sum
})
}

#[bench]
fn bench_range_step_by_loop_u32(b: &mut Bencher) {
let r = 0..(u16::MAX as u32);
b.iter(|| {
let r = black_box(r.clone()).step_by(64);

let mut sum: u32 = 0;
for i in r {
let i = i ^ i.wrapping_sub(1);
sum = sum.wrapping_add(i);
}

sum
})
}

#[bench]
fn bench_range_step_by_fold_usize(b: &mut Bencher) {
let r: Range<usize> = 0..(u16::MAX as usize);
b.iter(|| {
let r = black_box(r.clone());
r.step_by(64)
.map(|x: usize| x ^ (x.wrapping_sub(1)))
.fold(0usize, |acc, i| acc.wrapping_add(i))
})
}

#[bench]
fn bench_range_step_by_fold_u16(b: &mut Bencher) {
let r: Range<u16> = 0..u16::MAX;
b.iter(|| {
let r = black_box(r.clone());
r.step_by(64).map(|x: u16| x ^ (x.wrapping_sub(1))).fold(0u16, |acc, i| acc.wrapping_add(i))
})
}

pub fn copy_zip(xs: &[u8], ys: &mut [u8]) {
for (a, b) in ys.iter_mut().zip(xs) {
*a = *b;
Expand Down
Loading