File: bench.rs

package info (click to toggle)
rust-ucd-generate 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,528 kB
  • sloc: sh: 36; makefile: 2
file content (157 lines) | stat: -rw-r--r-- 3,912 bytes parent folder | download
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#![feature(test)]

extern crate test;

use std::cmp::Ordering;

use test::Bencher;

mod tables;

fn u32_key(cp: u32) -> [u8; 4] {
    cp.to_be_bytes()
}

#[bench]
fn general_category_slice(b: &mut Bencher) {
    let slice = tables::slice::general_category::GENERAL_CATEGORY;
    let mut i = 0;
    b.iter(|| {
        let (query, _, value) = slice[i];
        i = (i + 1) % slice.len();

        let pos = slice.binary_search_by(|&(s, e, _)| {
            if s > query {
                Ordering::Greater
            } else if e < query {
                Ordering::Less
            } else {
                Ordering::Equal
            }
        });
        let found = slice[pos.unwrap()];
        assert_eq!(found.2, value);
    });
}

#[bench]
fn general_category_fst(b: &mut Bencher) {
    let slice = tables::slice::general_category::GENERAL_CATEGORY;
    let fst = &tables::fst::general_category::GENERAL_CATEGORY;

    let mut i = 0;
    b.iter(|| {
        let (query, _, value) = slice[i];
        i = (i + 1) % slice.len();

        let found = fst.get(u32_key(query)).unwrap() as u8;
        assert_eq!(found, value);
    });
}

#[bench]
fn lowercase_letter_slice(b: &mut Bencher) {
    let slice = tables::slice::general_categories::LOWERCASE_LETTER;
    let mut i = 0;
    b.iter(|| {
        let (query, _) = slice[i];
        i = (i + 1) % slice.len();

        let pos = slice.binary_search_by(|&(s, e)| {
            if s > query {
                Ordering::Greater
            } else if e < query {
                Ordering::Less
            } else {
                Ordering::Equal
            }
        });
        assert!(pos.is_ok());
    });
}

#[bench]
fn lowercase_letter_trie(b: &mut Bencher) {
    let slice = tables::slice::general_categories::LOWERCASE_LETTER;
    let trie = tables::trie::general_categories::LOWERCASE_LETTER;
    let mut i = 0;
    b.iter(|| {
        let (query, _) = slice[i];
        i = (i + 1) % slice.len();
        assert!(trie.contains_u32(query));
    });
}

#[bench]
fn names_slice(b: &mut Bencher) {
    let slice = tables::slice::names::NAMES;
    let mut i = 0;
    b.iter(|| {
        let (name, cp) = slice[i];
        i = (i + 1) % slice.len();

        let found = slice[slice.binary_search_by_key(&name, |x| x.0).unwrap()];
        assert_eq!(found.1, cp);
    });
}

#[bench]
fn names_fst(b: &mut Bencher) {
    let slice = tables::slice::names::NAMES;
    let fst = &tables::fst::names::NAMES;

    let mut i = 0;
    b.iter(|| {
        let (name, cp) = slice[i];
        i = (i + 1) % slice.len();

        let found = fst.get(name).unwrap() as u32;
        assert_eq!(found, cp);
    });
}

#[bench]
fn jamo_short_name_fst(b: &mut Bencher) {
    let slice = tables::slice::jamo_short_name::JAMO_SHORT_NAME;
    let fst = &tables::fst::jamo_short_name::JAMO_SHORT_NAME;
    let mut i = 0;
    let mut value = String::new();
    b.iter(|| {
        let (cp, name) = slice[i];
        i = (i + 1) % slice.len();

        let mut found = fst.get(u32_key(cp)).unwrap();
        value.clear();
        while found != 0 {
            value.push((found & 0xFF) as u8 as char);
            found = found >> 8;
        }
        assert_eq!(value, name);
    });
}

#[bench]
fn jamo_short_name_slice(b: &mut Bencher) {
    let slice = tables::slice::jamo_short_name::JAMO_SHORT_NAME;
    let mut i = 0;
    b.iter(|| {
        let (cp, name) = slice[i];
        i = (i + 1) % slice.len();

        let found = slice[slice.binary_search_by_key(&cp, |x| x.0).unwrap()];
        assert_eq!(found.1, name);
    });
}

#[bench]
fn jamo_short_name_slice_linear(b: &mut Bencher) {
    let slice = tables::slice::jamo_short_name::JAMO_SHORT_NAME;
    let mut i = 0;
    b.iter(|| {
        let (cp, name) = slice[i];
        i = (i + 1) % slice.len();

        let found = slice.iter().find(|p| p.0 == cp).unwrap();
        assert_eq!(found.1, name);
    });
}