This repository was archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitAny_test.go
142 lines (130 loc) · 3 KB
/
splitAny_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
package main_test
import (
"strings"
"testing"
"github.com/jlortiz0/multisav/redditapi"
)
func splitAny(s string, seps string) []string {
out := make([]string, 0, len(s)*len(seps)/20+1)
ind := strings.IndexAny(s, seps)
for ind != -1 {
if ind != 0 {
out = append(out, s[:ind])
}
s = s[ind+1:]
ind = strings.IndexAny(s, seps)
}
if s != "" {
out = append(out, s)
}
return out
}
func splitAny2(s string, seps string) []string {
fast := make(map[rune]struct{}, len(seps))
for _, x := range seps {
fast[x] = struct{}{}
}
out := make([]string, 0, len(s)*len(seps)/20+1)
ind := strings.IndexFunc(s, func(r rune) bool { _, ok := fast[r]; return ok })
for ind != -1 {
if ind != 0 {
out = append(out, s[:ind])
}
s = s[ind+1:]
ind = strings.IndexFunc(s, func(r rune) bool { _, ok := fast[r]; return ok })
}
if s != "" {
out = append(out, s)
}
return out
}
func splitAny3(s string, seps string) []string {
fast := make([]bool, 256)
for _, x := range seps {
fast[x] = true
}
out := make([]string, 0, len(s)*len(seps)/20+1)
ind := strings.IndexFunc(s, func(r rune) bool { return fast[r] })
for ind != -1 {
if ind != 0 {
out = append(out, s[:ind])
}
s = s[ind+1:]
ind = strings.IndexFunc(s, func(r rune) bool { return fast[r] })
}
if s != "" {
out = append(out, s)
}
return out
}
func setupHelper(b *testing.B) string {
b.Helper()
red := redditapi.NewReddit("linux:org.jlortiz.multiSav:v0.7.0 (by /u/jlortiz)", RedditID, RedditSecret)
sub, err := red.Submission("hposam")
if err != nil {
panic(err)
}
if sub.Selftext == "" {
panic("no selftext")
}
b.ResetTimer()
return sub.Selftext
}
func BenchmarkSplitAny(b *testing.B) {
s := setupHelper(b)
b.SetBytes(int64(len(s)))
for i := 0; i < b.N; i++ {
splitAny(s, " \n\t()[]")
}
}
func BenchmarkSplitFunc(b *testing.B) {
s := setupHelper(b)
b.SetBytes(int64(len(s)))
for i := 0; i < b.N; i++ {
strings.FieldsFunc(s, func(c rune) bool { return strings.ContainsRune(" \n\t()[]", c) })
}
}
func BenchmarkSplitFunc2(b *testing.B) {
fast := make(map[rune]struct{}, 7)
fast[' '] = struct{}{}
fast['('] = struct{}{}
fast[')'] = struct{}{}
fast['['] = struct{}{}
fast[']'] = struct{}{}
fast['\n'] = struct{}{}
fast['\t'] = struct{}{}
s := setupHelper(b)
b.SetBytes(int64(len(s)))
for i := 0; i < b.N; i++ {
strings.FieldsFunc(s, func(c rune) bool { _, ok := fast[c]; return ok })
}
}
func BenchmarkSplitFunc3(b *testing.B) {
fast := make([]bool, 256)
fast[' '] = true
fast['('] = true
fast[')'] = true
fast['['] = true
fast[']'] = true
fast['\n'] = true
fast['\t'] = true
s := setupHelper(b)
b.SetBytes(int64(len(s)))
for i := 0; i < b.N; i++ {
strings.FieldsFunc(s, func(c rune) bool { return fast[c % 256] })
}
}
func BenchmarkSplitAny2(b *testing.B) {
s := setupHelper(b)
b.SetBytes(int64(len(s)))
for i := 0; i < b.N; i++ {
splitAny2(s, " \n\t()[]")
}
}
func BenchmarkSplitAny3(b *testing.B) {
s := setupHelper(b)
b.SetBytes(int64(len(s)))
for i := 0; i < b.N; i++ {
splitAny3(s, " \n\t()[]")
}
}