diff --git a/src/libcore/benches/char/methods.rs b/src/libcore/benches/char/methods.rs new file mode 100644 index 0000000000000..faf820d871cfa --- /dev/null +++ b/src/libcore/benches/char/methods.rs @@ -0,0 +1,42 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use test::Bencher; + +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) { + b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(2)).min()) +} + +#[bench] +fn bench_to_digit_radix_10(b: &mut Bencher) { + b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(10)).min()) +} + +#[bench] +fn bench_to_digit_radix_16(b: &mut Bencher) { + b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(16)).min()) +} + +#[bench] +fn bench_to_digit_radix_36(b: &mut Bencher) { + b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(36)).min()) +} + +#[bench] +fn bench_to_digit_radix_var(b: &mut Bencher) { + b.iter(|| CHARS.iter().cycle() + .zip(RADIX.iter().cycle()) + .take(10_000) + .map(|(c, radix)| c.to_digit(*radix)).min()) +} diff --git a/src/libcore/benches/char/mod.rs b/src/libcore/benches/char/mod.rs new file mode 100644 index 0000000000000..a656e82cb61e6 --- /dev/null +++ b/src/libcore/benches/char/mod.rs @@ -0,0 +1,11 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +mod methods; diff --git a/src/libcore/benches/lib.rs b/src/libcore/benches/lib.rs index ced77d779182a..d44f1577d56b0 100644 --- a/src/libcore/benches/lib.rs +++ b/src/libcore/benches/lib.rs @@ -15,6 +15,7 @@ extern crate core; extern crate test; mod any; +mod char; mod hash; mod iter; mod num; diff --git a/src/libcore/char/methods.rs b/src/libcore/char/methods.rs index 35181afea3da6..d6fcff644acf6 100644 --- a/src/libcore/char/methods.rs +++ b/src/libcore/char/methods.rs @@ -121,15 +121,24 @@ impl char { #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn to_digit(self, radix: u32) -> Option { - if radix > 36 { - panic!("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, + assert!(radix <= 36, "to_digit: radix is too high (maximum 36)"); + + // the code is split up here to improve execution speed for cases where + // the `radix` is constant and 10 or smaller + 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 } }