File: bench.rs

package info (click to toggle)
rust-utf8-width 0.1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 108 kB
  • sloc: makefile: 2
file content (142 lines) | stat: -rw-r--r-- 3,349 bytes parent folder | download | duplicates (2)
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
extern crate utf8_width;

#[macro_use]
extern crate bencher;

use std::fs;

use bencher::Bencher;

#[cfg(unix)]
const TEXT_PATH: &str = "benches/data/wikipedia-rust.txt";

#[cfg(windows)]
const TEXT_PATH: &str = r"benches\data\wikipedia-rust.txt";

static UTF8_CHAR_WIDTH: [usize; 256] = [
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, // 0x1F
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, // 0x3F
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, // 0x5F
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, // 0x7F
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, // 0x9F
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, // 0xBF
    0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, // 0xDF
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xEF
    4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xFF
];

fn retrieve_get_width(bencher: &mut Bencher) {
    let bytes = fs::read(TEXT_PATH).unwrap();
    let length = bytes.len();

    bencher.iter(|| {
        let mut widths = Vec::new();

        let mut p = 0;

        loop {
            let e = bytes[p];

            let width = utf8_width::get_width(e);

            widths.push(width);

            p += width;

            if p == length {
                break;
            }
        }

        widths
    });

    bencher.bytes = length as u64;
}

fn retrieve_get_width_assume_valid(bencher: &mut Bencher) {
    let bytes = fs::read(TEXT_PATH).unwrap();
    let length = bytes.len();

    bencher.iter(|| {
        let mut widths = Vec::new();

        let mut p = 0;
        let length = bytes.len();

        loop {
            let e = bytes[p];

            let width = unsafe { utf8_width::get_width_assume_valid(e) };

            widths.push(width);

            p += width;

            if p == length {
                break;
            }
        }

        widths
    });

    bencher.bytes = length as u64;
}

fn retrieve_get_width_by_looking_table(bencher: &mut Bencher) {
    let bytes = fs::read(TEXT_PATH).unwrap();
    let length = bytes.len();

    bencher.iter(|| {
        let mut widths = Vec::new();

        let mut p = 0;
        let length = bytes.len();

        loop {
            let e = bytes[p];

            let width = UTF8_CHAR_WIDTH[e as usize];

            widths.push(width);

            p += width;

            if p == length {
                break;
            }
        }

        widths
    });

    bencher.bytes = length as u64;
}

fn retrieve_get_width_by_chars(bencher: &mut Bencher) {
    let text = fs::read_to_string(TEXT_PATH).unwrap();
    let length = text.len();

    bencher.iter(|| {
        let mut widths = Vec::new();

        for c in text.chars() {
            widths.push(c.len_utf8())
        }

        widths
    });

    bencher.bytes = length as u64;
}

benchmark_group!(get_width, retrieve_get_width, retrieve_get_width_assume_valid, retrieve_get_width_by_looking_table, retrieve_get_width_by_chars);
benchmark_main!(get_width);