2023-02-02 09:43:07 -06:00
|
|
|
use test::{black_box, Bencher};
|
2018-11-13 11:03:06 -06:00
|
|
|
|
|
|
|
const CHARS: [char; 9] = ['0', 'x', '2', '5', 'A', 'f', '7', '8', '9'];
|
|
|
|
const RADIX: [u32; 5] = [2, 8, 10, 16, 32];
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_to_digit_radix_2(b: &mut Bencher) {
|
2023-02-02 09:43:07 -06:00
|
|
|
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| black_box(c).to_digit(2)).min())
|
2018-11-13 11:03:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_to_digit_radix_10(b: &mut Bencher) {
|
2023-02-02 09:43:07 -06:00
|
|
|
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| black_box(c).to_digit(10)).min())
|
2018-11-13 11:03:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_to_digit_radix_16(b: &mut Bencher) {
|
2023-02-02 09:43:07 -06:00
|
|
|
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| black_box(c).to_digit(16)).min())
|
2018-11-13 11:03:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_to_digit_radix_36(b: &mut Bencher) {
|
2023-02-02 09:43:07 -06:00
|
|
|
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| black_box(c).to_digit(36)).min())
|
2018-11-13 11:03:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_to_digit_radix_var(b: &mut Bencher) {
|
2019-12-06 22:18:12 -06:00
|
|
|
b.iter(|| {
|
|
|
|
CHARS
|
|
|
|
.iter()
|
|
|
|
.cycle()
|
|
|
|
.zip(RADIX.iter().cycle())
|
|
|
|
.take(10_000)
|
2023-02-02 09:43:07 -06:00
|
|
|
.map(|(c, radix)| black_box(c).to_digit(*radix))
|
2019-12-06 22:18:12 -06:00
|
|
|
.min()
|
|
|
|
})
|
2018-11-13 11:03:06 -06:00
|
|
|
}
|
2021-02-06 13:14:13 -06:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_to_ascii_uppercase(b: &mut Bencher) {
|
2023-02-02 09:43:07 -06:00
|
|
|
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| black_box(c).to_ascii_uppercase()).min())
|
2021-02-06 13:14:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_to_ascii_lowercase(b: &mut Bencher) {
|
2023-02-02 09:43:07 -06:00
|
|
|
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| black_box(c).to_ascii_lowercase()).min())
|
2021-02-06 13:14:13 -06:00
|
|
|
}
|
2021-01-24 20:29:44 -06:00
|
|
|
|
|
|
|
#[bench]
|
2021-02-02 20:18:03 -06:00
|
|
|
fn bench_ascii_mix_to_uppercase(b: &mut Bencher) {
|
2023-02-02 09:43:07 -06:00
|
|
|
b.iter(|| {
|
|
|
|
(0..=255).cycle().take(10_000).map(|b| black_box(char::from(b)).to_uppercase()).count()
|
|
|
|
})
|
2021-01-24 20:29:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2021-02-02 20:18:03 -06:00
|
|
|
fn bench_ascii_mix_to_lowercase(b: &mut Bencher) {
|
2023-02-02 09:43:07 -06:00
|
|
|
b.iter(|| {
|
|
|
|
(0..=255).cycle().take(10_000).map(|b| black_box(char::from(b)).to_lowercase()).count()
|
|
|
|
})
|
2021-01-24 20:29:44 -06:00
|
|
|
}
|
2021-02-02 20:18:03 -06:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_ascii_char_to_uppercase(b: &mut Bencher) {
|
2023-02-02 09:43:07 -06:00
|
|
|
b.iter(|| {
|
|
|
|
(0..=127).cycle().take(10_000).map(|b| black_box(char::from(b)).to_uppercase()).count()
|
|
|
|
})
|
2021-02-02 20:18:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_ascii_char_to_lowercase(b: &mut Bencher) {
|
2023-02-02 09:43:07 -06:00
|
|
|
b.iter(|| {
|
|
|
|
(0..=127).cycle().take(10_000).map(|b| black_box(char::from(b)).to_lowercase()).count()
|
|
|
|
})
|
2021-02-02 20:18:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_non_ascii_char_to_uppercase(b: &mut Bencher) {
|
2023-02-02 09:43:07 -06:00
|
|
|
b.iter(|| {
|
|
|
|
(128..=255).cycle().take(10_000).map(|b| black_box(char::from(b)).to_uppercase()).count()
|
|
|
|
})
|
2021-02-02 20:18:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_non_ascii_char_to_lowercase(b: &mut Bencher) {
|
2023-02-02 09:43:07 -06:00
|
|
|
b.iter(|| {
|
|
|
|
(128..=255).cycle().take(10_000).map(|b| black_box(char::from(b)).to_lowercase()).count()
|
|
|
|
})
|
2021-02-02 20:18:03 -06:00
|
|
|
}
|