| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use criterion::{criterion_group, criterion_main, Criterion}; |
| 6 |
use icu_collections::codepointinvlist::CodePointInversionList; |
| 7 |
use std::char; |
| 8 |
|
| 9 |
fn uniset_bench(c: &mut Criterion) { |
| 10 |
let best_ex = [0x41, 0x46]; |
| 11 |
let best_sample = CodePointInversionList::try_from_inversion_list_slice(&best_ex).unwrap(); |
| 12 |
let worst_ex: Vec<u32> = (0x0..((char::MAX as u32) + 1)).collect(); |
| 13 |
let worst_sample = CodePointInversionList::try_from_inversion_list_slice(&worst_ex).unwrap(); |
| 14 |
|
| 15 |
c.bench_function("uniset/overview", |b| { |
| 16 |
#[allow(clippy::suspicious_map)] |
| 17 |
b.iter(|| { |
| 18 |
best_sample |
| 19 |
.iter_chars() |
| 20 |
.map(|ch| best_sample.contains(ch)) |
| 21 |
.count(); |
| 22 |
worst_sample |
| 23 |
.iter_chars() |
| 24 |
.map(|ch| worst_sample.contains(ch)) |
| 25 |
.count(); |
| 26 |
best_sample |
| 27 |
.iter_chars() |
| 28 |
.map(|ch| best_sample.contains_range(&('A'..ch))) |
| 29 |
.count(); |
| 30 |
worst_sample |
| 31 |
.iter_chars() |
| 32 |
.take(100) |
| 33 |
.map(|ch| worst_sample.contains_range(&(char::from_u32(0x0).unwrap()..ch))) |
| 34 |
.count(); |
| 35 |
}) |
| 36 |
}); |
| 37 |
|
| 38 |
#[cfg(feature = "bench")] |
| 39 |
{ |
| 40 |
let mut group = c.benchmark_group("uniset/contains"); |
| 41 |
group.bench_with_input("best", &best_sample, |b, sample| { |
| 42 |
b.iter(|| sample.iter_chars().map(|ch| sample.contains(ch))) |
| 43 |
}); |
| 44 |
group.bench_with_input("worst", &worst_sample, |b, sample| { |
| 45 |
b.iter(|| sample.iter_chars().take(100).map(|ch| sample.contains(ch))) |
| 46 |
}); |
| 47 |
group.finish(); |
| 48 |
|
| 49 |
let mut group = c.benchmark_group("uniset/contains_range"); |
| 50 |
group.bench_with_input("best", &best_sample, |b, sample| { |
| 51 |
b.iter(|| { |
| 52 |
sample |
| 53 |
.iter_chars() |
| 54 |
.map(|ch| sample.contains_range(&('A'..ch))) |
| 55 |
}) |
| 56 |
}); |
| 57 |
group.bench_with_input("worst", &worst_sample, |b, sample| { |
| 58 |
b.iter(|| { |
| 59 |
sample |
| 60 |
.iter_chars() |
| 61 |
.take(100) |
| 62 |
.map(|ch| sample.contains_range(&(char::from_u32(0x0).unwrap()..ch))) |
| 63 |
}) |
| 64 |
}); |
| 65 |
group.finish(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
criterion_group!(benches, uniset_bench); |
| 70 |
criterion_main!(benches); |