mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-10 02:56:52 +00:00
core/char: Speed up to_digit()
for radix <= 10
### Before ``` # Run 1 test char::methods::bench_to_digit_radix_10 ... bench: 16,265 ns/iter (+/- 1,774) test char::methods::bench_to_digit_radix_16 ... bench: 13,938 ns/iter (+/- 2,479) test char::methods::bench_to_digit_radix_2 ... bench: 13,090 ns/iter (+/- 524) test char::methods::bench_to_digit_radix_36 ... bench: 14,236 ns/iter (+/- 1,949) # Run 2 test char::methods::bench_to_digit_radix_10 ... bench: 16,176 ns/iter (+/- 1,589) test char::methods::bench_to_digit_radix_16 ... bench: 13,896 ns/iter (+/- 3,140) test char::methods::bench_to_digit_radix_2 ... bench: 13,158 ns/iter (+/- 1,112) test char::methods::bench_to_digit_radix_36 ... bench: 14,206 ns/iter (+/- 1,312) # Run 3 test char::methods::bench_to_digit_radix_10 ... bench: 16,221 ns/iter (+/- 2,423) test char::methods::bench_to_digit_radix_16 ... bench: 14,361 ns/iter (+/- 3,926) test char::methods::bench_to_digit_radix_2 ... bench: 13,097 ns/iter (+/- 671) test char::methods::bench_to_digit_radix_36 ... bench: 14,388 ns/iter (+/- 1,068) ``` ### After ``` # Run 1 test char::methods::bench_to_digit_radix_10 ... bench: 11,521 ns/iter (+/- 552) test char::methods::bench_to_digit_radix_16 ... bench: 12,926 ns/iter (+/- 684) test char::methods::bench_to_digit_radix_2 ... bench: 11,266 ns/iter (+/- 1,085) test char::methods::bench_to_digit_radix_36 ... bench: 14,213 ns/iter (+/- 614) # Run 2 test char::methods::bench_to_digit_radix_10 ... bench: 11,424 ns/iter (+/- 1,042) test char::methods::bench_to_digit_radix_16 ... bench: 12,854 ns/iter (+/- 1,193) test char::methods::bench_to_digit_radix_2 ... bench: 11,193 ns/iter (+/- 716) test char::methods::bench_to_digit_radix_36 ... bench: 14,249 ns/iter (+/- 3,514) # Run 3 test char::methods::bench_to_digit_radix_10 ... bench: 11,469 ns/iter (+/- 685) test char::methods::bench_to_digit_radix_16 ... bench: 12,852 ns/iter (+/- 568) test char::methods::bench_to_digit_radix_2 ... bench: 11,275 ns/iter (+/- 1,356) test char::methods::bench_to_digit_radix_36 ... bench: 14,188 ns/iter (+/- 1,501) ```
This commit is contained in:
parent
04aade83f2
commit
17f08fecfd
@ -122,12 +122,27 @@ impl char {
|
||||
#[inline]
|
||||
pub fn to_digit(self, radix: u32) -> Option<u32> {
|
||||
assert!(radix <= 36, "to_digit: radix is too high (maximum 36)");
|
||||
let val = match self {
|
||||
'0' ..= '9' => self as u32 - '0' as u32,
|
||||
'a' ..= 'z' => self as u32 - 'a' as u32 + 10,
|
||||
'A' ..= 'Z' => self as u32 - 'A' as u32 + 10,
|
||||
_ => return None,
|
||||
if radix == 10 {
|
||||
return match self {
|
||||
'0' ..= '9' => Some(self as u32 - '0' as u32),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
|
||||
let val = if radix < 10 {
|
||||
match self {
|
||||
'0' ..= '9' => self as u32 - '0' as u32,
|
||||
_ => return None,
|
||||
}
|
||||
} else {
|
||||
match self {
|
||||
'0'..='9' => self as u32 - '0' as u32,
|
||||
'a'..='z' => self as u32 - 'a' as u32 + 10,
|
||||
'A'..='Z' => self as u32 - 'A' as u32 + 10,
|
||||
_ => return None,
|
||||
}
|
||||
};
|
||||
|
||||
if val < radix { Some(val) }
|
||||
else { None }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user