-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuniseg_bench_test.go
More file actions
73 lines (68 loc) · 1.5 KB
/
uniseg_bench_test.go
File metadata and controls
73 lines (68 loc) · 1.5 KB
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
package vaxis_test
import (
"strings"
"testing"
"github.com/rivo/uniseg"
)
func BenchmarkUniseg(b *testing.B) {
const testString = "😀🔮🌏📝test string"
b.Run("rune reader", func(b *testing.B) {
// Just so we can see the penalty
for i := 0; i < b.N; i += 1 {
result := []string{}
r := strings.NewReader(testString)
for {
ch, _, err := r.ReadRune()
if err != nil {
break
}
result = append(result, string(ch))
}
}
})
b.Run("graphemes", func(b *testing.B) {
for i := 0; i < b.N; i += 1 {
result := []string{}
g := uniseg.NewGraphemes(testString)
for g.Next() {
result = append(result, g.Str())
}
}
})
b.Run("step", func(b *testing.B) {
for i := 0; i < b.N; i += 1 {
result := [][]byte{}
in := []byte(testString)
state := -1
cluster := []byte{}
for len(in) > 0 {
cluster, in, _, state = uniseg.Step(in, state)
result = append(result, cluster)
}
}
})
b.Run("stepstring", func(b *testing.B) {
for i := 0; i < b.N; i += 1 {
result := []string{}
in := testString
state := -1
cluster := ""
for len(in) > 0 {
cluster, in, _, state = uniseg.StepString(in, state)
result = append(result, cluster)
}
}
})
b.Run("firstfunction", func(b *testing.B) {
for i := 0; i < b.N; i += 1 {
result := []string{}
in := testString
state := -1
cluster := ""
for len(in) > 0 {
cluster, in, _, state = uniseg.FirstGraphemeClusterInString(in, state)
result = append(result, cluster)
}
}
})
}