-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstring_test.go
127 lines (108 loc) · 2.35 KB
/
string_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
package logbus
import (
"bytes"
"fmt"
"strings"
"testing"
)
// https://gist.github.com/dtjm/c6ebc86abe7515c988ec
// go test -v -run=BENCH -bench=. -benchtime 5s -benchmem
var (
testData = []string{"a", "b", "c", "d", "e"}
)
func BenchmarkJoin(b *testing.B) {
for i := 0; i < b.N; i++ {
s := strings.Join(testData, ":")
_ = s
}
}
func BenchmarkSprintf(b *testing.B) {
for i := 0; i < b.N; i++ {
s := fmt.Sprintf("%s:%s:%s:%s:%s", testData[0], testData[1], testData[2], testData[3], testData[4])
_ = s
}
}
func BenchmarkConcat(b *testing.B) {
for i := 0; i < b.N; i++ {
s := testData[0] + ":"
s += testData[1] + ":"
s += testData[2] + ":"
s += testData[3] + ":"
s += testData[4]
_ = s
}
}
func BenchmarkConcatOneLine(b *testing.B) {
for i := 0; i < b.N; i++ {
s := testData[0] + ":" +
testData[1] + ":" +
testData[2] + ":" +
testData[3] + ":" +
testData[4]
_ = s
}
}
func BenchmarkBuffer(b *testing.B) {
for i := 0; i < b.N; i++ {
var b bytes.Buffer
b.WriteString(testData[0])
b.WriteByte(':')
b.WriteString(testData[1])
b.WriteByte(':')
b.WriteString(testData[2])
b.WriteByte(':')
b.WriteString(testData[3])
b.WriteByte(':')
b.WriteString(testData[4])
s := b.String()
_ = s
}
}
func BenchmarkBufferWithReset(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
buf.Reset()
buf.WriteString(testData[0])
buf.WriteByte(':')
buf.WriteString(testData[1])
buf.WriteByte(':')
buf.WriteString(testData[2])
buf.WriteByte(':')
buf.WriteString(testData[3])
buf.WriteByte(':')
buf.WriteString(testData[4])
s := buf.String()
_ = s
}
}
func BenchmarkBufferFprintf(b *testing.B) {
buf := &bytes.Buffer{}
for i := 0; i < b.N; i++ {
buf.Reset()
fmt.Fprintf(buf, "%s:%s:%s:%s:%s", testData[0], testData[1], testData[2], testData[3], testData[4])
s := buf.String()
_ = s
}
}
func BenchmarkBufferStringBuilder(b *testing.B) {
var buf strings.Builder
for i := 0; i < b.N; i++ {
buf.Reset()
buf.WriteString(testData[0])
buf.WriteByte(':')
buf.WriteString(testData[1])
buf.WriteByte(':')
buf.WriteString(testData[2])
buf.WriteByte(':')
buf.WriteString(testData[3])
buf.WriteByte(':')
buf.WriteString(testData[4])
s := buf.String()
_ = s
}
}
func BenchmarkDebug(b *testing.B) {
for i := 0; i < b.N; i++ {
Debug("", String("a", "b"), String("C", "D"))
}
}