summaryrefslogtreecommitdiffstats
path: root/third_party/rust/unicode-xid/benches/xid.rs
blob: a565c36e1703f7a5461b2a0f259a26560f9c77fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
extern crate criterion;
extern crate unicode_xid;

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use unicode_xid::UnicodeXID;

fn bench_unicode_xid(c: &mut Criterion) {
    let unicode_chars = chars(1..0x3000);
    let ascii_chars = chars(1..0x80);

    let mut group = c.benchmark_group("UnicodeXID");
    group.throughput(Throughput::Bytes(unicode_chars.len() as u64));
    group.bench_with_input(
        BenchmarkId::new("is_xid_start", "unicode"),
        &unicode_chars,
        |b, chars| b.iter(|| chars.iter().copied().map(UnicodeXID::is_xid_start).last()),
    );
    group.throughput(Throughput::Bytes(ascii_chars.len() as u64));
    group.bench_with_input(
        BenchmarkId::new("is_xid_start", "ascii"),
        &ascii_chars,
        |b, chars| b.iter(|| chars.iter().copied().map(UnicodeXID::is_xid_start).last()),
    );
    group.throughput(Throughput::Bytes(unicode_chars.len() as u64));
    group.bench_with_input(
        BenchmarkId::new("is_xid_continue", "unicode"),
        &unicode_chars,
        |b, chars| {
            b.iter(|| {
                chars
                    .iter()
                    .copied()
                    .map(UnicodeXID::is_xid_continue)
                    .last()
            })
        },
    );
    group.throughput(Throughput::Bytes(ascii_chars.len() as u64));
    group.bench_with_input(
        BenchmarkId::new("is_xid_continue", "ascii"),
        &ascii_chars,
        |b, chars| {
            b.iter(|| {
                chars
                    .iter()
                    .copied()
                    .map(UnicodeXID::is_xid_continue)
                    .last()
            })
        },
    );
    group.finish();
}

fn chars(range: std::ops::Range<u32>) -> Vec<char> {
    range.filter_map(|i| std::char::from_u32(i)).collect()
}

criterion_group!(benches, bench_unicode_xid);
criterion_main!(benches);