1 use criterion
::black_box
;
2 use criterion
::criterion_group
;
3 use criterion
::criterion_main
;
4 use criterion
::Bencher
;
5 use criterion
::Criterion
;
8 use tinystr
::{TinyStr16, TinyStr4, TinyStr8}
;
10 static STRINGS_4
: &[&str] = &[
11 "US", "GB", "AR", "Hans", "CN", "AT", "PL", "FR", "AT", "Cyrl", "SR", "NO", "FR", "MK", "UK",
14 static STRINGS_8
: &[&str] = &[
15 "Latn", "windows", "AR", "Hans", "macos", "AT", "pl", "FR", "en", "Cyrl", "SR", "NO", "419",
19 static STRINGS_16
: &[&str] = &[
37 macro_rules
! bench_block
{
38 ($c
:expr
, $name
:expr
, $action
:ident
) => {
40 Fun
::new("String", $action
!(String
)),
41 Fun
::new("TinyStr4", $action
!(TinyStr4
)),
42 Fun
::new("TinyStr8", $action
!(TinyStr8
)),
43 Fun
::new("TinyStr16", $action
!(TinyStr16
)),
46 $c
.bench_functions(&format
!("{}/4", $name
), funcs
, STRINGS_4
);
49 Fun
::new("String", $action
!(String
)),
50 Fun
::new("TinyStr8", $action
!(TinyStr8
)),
51 Fun
::new("TinyStr16", $action
!(TinyStr16
)),
54 $c
.bench_functions(&format
!("{}/8", $name
), funcs
, STRINGS_8
);
57 Fun
::new("String", $action
!(String
)),
58 Fun
::new("TinyStr16", $action
!(TinyStr16
)),
61 $c
.bench_functions(&format
!("{}/16", $name
), funcs
, STRINGS_16
);
65 macro_rules
! convert_to_ascii
{
66 ($ty
:ty
, $action
:ident
) => {
67 |b
: &mut Bencher
, inputs
: &&[&str]| {
68 let raw
: Vec
<$ty
> = inputs
.iter().map(|s
| s
.parse
::<$ty
>().unwrap()).collect();
71 let _
= black_box(s
.$
action());
78 fn convert_to_ascii_lowercase(c
: &mut Criterion
) {
81 convert_to_ascii
!($ty
, to_ascii_lowercase
)
85 bench_block
!(c
, "convert_to_ascii_lowercase", ctal
);
88 fn convert_to_ascii_uppercase(c
: &mut Criterion
) {
91 convert_to_ascii
!($ty
, to_ascii_uppercase
)
95 bench_block
!(c
, "convert_to_ascii_uppercase", ctau
);
98 trait ExtToAsciiTitlecase
{
100 fn to_ascii_titlecase(&self) -> String
;
103 impl ExtToAsciiTitlecase
for str {
104 fn to_ascii_titlecase(&self) -> String
{
105 let mut result
= self.to_ascii_lowercase();
106 result
[0..1].make_ascii_uppercase();
111 fn convert_to_ascii_titlecase(c
: &mut Criterion
) {
114 convert_to_ascii
!($ty
, to_ascii_titlecase
)
118 bench_block
!(c
, "convert_to_ascii_titlecase", ctat
);
121 trait ExtIsAsciiAlphanumeric
{
123 fn is_ascii_alphanumeric(&self) -> bool
;
126 impl ExtIsAsciiAlphanumeric
for str {
127 fn is_ascii_alphanumeric(&self) -> bool
{
128 self.chars().all(|c
| c
.is_ascii_alphanumeric())
132 fn test_is_ascii_alphanumeric(c
: &mut Criterion
) {
135 |b
: &mut Bencher
, inputs
: &&[&str]| {
136 let raw
: Vec
<$ty
> = inputs
.iter().map(|s
| s
.parse
::<$ty
>().unwrap()).collect();
139 let _
= black_box(s
.is_ascii_alphanumeric());
146 bench_block
!(c
, "test_is_ascii_alphanumeric", tiaa
);
149 fn test_eq(c
: &mut Criterion
) {
152 |b
: &mut Bencher
, inputs
: &&[&str]| {
153 let raw
: Vec
<$ty
> = inputs
.iter().map(|s
| s
.parse
::<$ty
>().unwrap()).collect();
157 let _
= black_box(s
== l
);
165 bench_block
!(c
, "test_eq", te
);
170 convert_to_ascii_lowercase
,
171 convert_to_ascii_uppercase
,
172 convert_to_ascii_titlecase
,
173 test_is_ascii_alphanumeric
,
176 criterion_main
!(benches
);