Skip to content

Commit 98f61a3

Browse files
committed
core/benches: Add char::to_digit() benchmarks
1 parent 5c9f7dc commit 98f61a3

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/libcore/benches/char/methods.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

src/libcore/benches/char/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
mod methods;

src/libcore/benches/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern crate core;
1515
extern crate test;
1616

1717
mod any;
18+
mod char;
1819
mod hash;
1920
mod iter;
2021
mod num;

0 commit comments

Comments
 (0)