-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_joint_test.go
87 lines (77 loc) · 1.69 KB
/
string_joint_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
package main
import (
"bytes"
"fmt"
"strconv"
"strings"
"testing"
)
func stringAdd(num int) string {
numbers := 100
var str string
for i := 0; i < num; i++ {
str = "" //保证与其他函数拼接效果一致
for j := 0; j < numbers; j++ {
str += strconv.Itoa(j)
}
}
return str
}
func stringBuffer(num int) (str string) {
numbers := 100
for i := 0; i < num; i++ {
var buffer bytes.Buffer
for j := 0; j < numbers; j++ {
buffer.WriteString(strconv.Itoa(j))
}
str = buffer.String()
}
return str
}
func stringBuilder(num int) (str string) {
numbers := 100
for i := 0; i < num; i++ {
var builder strings.Builder
for j := 0; j < numbers; j++ {
builder.WriteString(strconv.Itoa(j))
}
str = builder.String()
}
return str
}
func BenchmarkStringAdd(b *testing.B) {
b.ResetTimer()
stringAdd(b.N)
b.StopTimer()
}
func BenchmarkStringBuffer(b *testing.B) {
b.ResetTimer()
stringBuffer(b.N)
b.StopTimer()
}
func BenchmarkStringBuilder(b *testing.B) {
b.ResetTimer()
stringBuilder(b.N)
b.StopTimer()
}
func main() {
//想要查看打印结果,可以将文件名改为man.go
strAdd := stringAdd(100)
fmt.Println(strAdd)
fmt.Println("-----------------")
strBuffer := stringBuffer(100)
fmt.Println(strBuffer)
fmt.Println("-----------------")
strBuilder := stringBuilder(100)
fmt.Println(strBuilder)
}
//结论:性能依次降低
//Builder > Buffer > add+
//推荐使用 Builder进行字符串拼接
//执行测试 go test -bench=.
//goos: darwin
//goarch: amd64
//pkg: go-study/performance.test
//BenchmarkStringAdd-8 204414 5677 ns/op
//BenchmarkStringBuffer-8 1000000 1093 ns/op
//BenchmarkStringBuilder-8 1330159 902 ns/op