forked from mattn/go-runewidth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_test.go
166 lines (158 loc) · 3.66 KB
/
benchmark_test.go
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
158
159
160
161
162
163
164
165
166
package runewidth
import (
"testing"
"unicode/utf8"
)
var benchSink int
//
// RuneWidth
//
func benchRuneWidth(b *testing.B, eastAsianWidth bool, start, stop rune, want int) int {
b.Helper()
n := 0
b.Run("regular", func(b *testing.B) {
got := -1
c := NewCondition()
c.EastAsianWidth = eastAsianWidth
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
got = n
for r := start; r < stop; r++ {
n += c.RuneWidth(r)
}
got = n - got
}
if want != 0 && got != want { // some extra checks
b.Errorf("got %d, want %d\n", got, want)
}
})
b.Run("lut", func(b *testing.B) {
got := -1
n = 0
c := NewCondition()
c.EastAsianWidth = eastAsianWidth
c.CreateLUT()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
got = n
for r := start; r < stop; r++ {
n += c.RuneWidth(r)
}
got = n - got
}
if want != 0 && got != want { // some extra checks
b.Errorf("got %d, want %d\n", got, want)
}
})
return n
}
func BenchmarkRuneWidthAll(b *testing.B) {
benchSink = benchRuneWidth(b, false, 0, utf8.MaxRune+1, 1293942)
}
func BenchmarkRuneWidth768(b *testing.B) {
benchSink = benchRuneWidth(b, false, 0, 0x300, 702)
}
func BenchmarkRuneWidthAllEastAsian(b *testing.B) {
benchSink = benchRuneWidth(b, true, 0, utf8.MaxRune+1, 1432568)
}
func BenchmarkRuneWidth768EastAsian(b *testing.B) {
benchSink = benchRuneWidth(b, true, 0, 0x300, 794)
}
//
// String1Width - strings which consist of a single rune
//
func benchString1Width(b *testing.B, eastAsianWidth bool, start, stop rune, want int) int {
b.Helper()
n := 0
b.Run("regular", func(b *testing.B) {
got := -1
c := NewCondition()
c.EastAsianWidth = eastAsianWidth
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
got = n
for r := start; r < stop; r++ {
s := string(r)
n += c.StringWidth(s)
}
got = n - got
}
if want != 0 && got != want { // some extra checks
b.Errorf("got %d, want %d\n", got, want)
}
})
b.Run("lut", func(b *testing.B) {
got := -1
n = 0
c := NewCondition()
c.EastAsianWidth = eastAsianWidth
c.CreateLUT()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
got = n
for r := start; r < stop; r++ {
s := string(r)
n += c.StringWidth(s)
}
got = n - got
}
if want != 0 && got != want { // some extra checks
b.Errorf("got %d, want %d\n", got, want)
}
})
return n
}
func BenchmarkString1WidthAll(b *testing.B) {
benchSink = benchString1Width(b, false, 0, utf8.MaxRune+1, 1295990)
}
func BenchmarkString1Width768(b *testing.B) {
benchSink = benchString1Width(b, false, 0, 0x300, 702)
}
func BenchmarkString1WidthAllEastAsian(b *testing.B) {
benchSink = benchString1Width(b, true, 0, utf8.MaxRune+1, 1436664)
}
func BenchmarkString1Width768EastAsian(b *testing.B) {
benchSink = benchString1Width(b, true, 0, 0x300, 794)
}
//
// tables
//
func benchTable(b *testing.B, tbl table) int {
n := 0
for i := 0; i < b.N; i++ {
for r := rune(0); r <= utf8.MaxRune; r++ {
if inTable(r, tbl) {
n++
}
}
}
return n
}
func BenchmarkTablePrivate(b *testing.B) {
benchSink = benchTable(b, private)
}
func BenchmarkTableNonprint(b *testing.B) {
benchSink = benchTable(b, nonprint)
}
func BenchmarkTableCombining(b *testing.B) {
benchSink = benchTable(b, combining)
}
func BenchmarkTableDoublewidth(b *testing.B) {
benchSink = benchTable(b, doublewidth)
}
func BenchmarkTableAmbiguous(b *testing.B) {
benchSink = benchTable(b, ambiguous)
}
func BenchmarkTableEmoji(b *testing.B) {
benchSink = benchTable(b, emoji)
}
func BenchmarkTableNarrow(b *testing.B) {
benchSink = benchTable(b, narrow)
}
func BenchmarkTableNeutral(b *testing.B) {
benchSink = benchTable(b, neutral)
}