|  | 
|  | 1 | +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | 
|  | 2 | +// file at the top-level directory of this distribution and at | 
|  | 3 | +// http://rust-lang.org/COPYRIGHT. | 
|  | 4 | +// | 
|  | 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | 
|  | 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | 
|  | 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | 
|  | 8 | +// option. This file may not be copied, modified, or distributed | 
|  | 9 | +// except according to those terms. | 
|  | 10 | + | 
|  | 11 | +use test::Bencher; | 
|  | 12 | + | 
|  | 13 | +const CHARS: [char; 9] = ['0', 'x', '2', '5', 'A', 'f', '7', '8', '9']; | 
|  | 14 | +const RADIX: [u32; 5] = [2, 8, 10, 16, 32]; | 
|  | 15 | + | 
|  | 16 | +#[bench] | 
|  | 17 | +fn bench_to_digit_radix_2(b: &mut Bencher) { | 
|  | 18 | +    b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(2)).min()) | 
|  | 19 | +} | 
|  | 20 | + | 
|  | 21 | +#[bench] | 
|  | 22 | +fn bench_to_digit_radix_10(b: &mut Bencher) { | 
|  | 23 | +    b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(10)).min()) | 
|  | 24 | +} | 
|  | 25 | + | 
|  | 26 | +#[bench] | 
|  | 27 | +fn bench_to_digit_radix_16(b: &mut Bencher) { | 
|  | 28 | +    b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(16)).min()) | 
|  | 29 | +} | 
|  | 30 | + | 
|  | 31 | +#[bench] | 
|  | 32 | +fn bench_to_digit_radix_36(b: &mut Bencher) { | 
|  | 33 | +    b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(36)).min()) | 
|  | 34 | +} | 
|  | 35 | + | 
|  | 36 | +#[bench] | 
|  | 37 | +fn bench_to_digit_radix_var(b: &mut Bencher) { | 
|  | 38 | +    b.iter(|| CHARS.iter().cycle() | 
|  | 39 | +        .zip(RADIX.iter().cycle()) | 
|  | 40 | +        .take(10_000) | 
|  | 41 | +        .map(|(c, radix)| c.to_digit(*radix)).min()) | 
|  | 42 | +} | 
0 commit comments