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
| package hello
import (
"bytes"
"fmt"
"strings"
"testing"
)
const (
src = "Hello World!"
cnt = 10000
)
var expected = strings.Repeat(src, cnt)
func mycopy(b []byte, s string) []byte {
n := len(b)
if n+len(s) > cap(b) {
t := make([]byte, 2*cap(b)+len(s))
copy(t, b)
b = t
}
b = b[0 : n+len(s)]
copy(b[n:], s)
return b
}
func BenchmarkStringConcat(b *testing.B) {
var res string
for n := 0; n < b.N; n++ {
var str string
for i := 0; i < cnt; i++ {
str += src
}
res = str
}
b.StopTimer()
if res != expected {
b.Errorf("got=%s, want=%s", string(res), expected)
}
}
func BenchmarkStringSprintf(b *testing.B) {
var res string
for n := 0; n < b.N; n++ {
var str string
for i := 0; i < cnt; i++ {
str = fmt.Sprintf("%s%s", str, src)
}
res = str
}
b.StopTimer()
if res != expected {
b.Errorf("got=%s, want=%s", string(res), expected)
}
}
func BenchmarkStringJoin(b *testing.B) {
var res string
for n := 0; n < b.N; n++ {
var str string
for i := 0; i < cnt; i++ {
str = strings.Join([]string{str, src}, "")
}
res = str
}
b.StopTimer()
if res != expected {
b.Errorf("got=%s, want=%s", res, expected)
}
}
func BenchmarkBufferWrite(b *testing.B) {
var res string
for n := 0; n < b.N; n++ {
buf := new(bytes.Buffer)
for i := 0; i < cnt; i++ {
buf.WriteString(src)
}
res = buf.String()
}
b.StopTimer()
if res != expected {
b.Errorf("got=%s, want=%s", string(res), expected)
}
}
func BenchmarkBytesAppend(b *testing.B) {
var res string
for n := 0; n < b.N; n++ {
var bytes []byte
for i := 0; i < cnt; i++ {
bytes = append(bytes, src...)
}
res = string(bytes)
}
b.StopTimer()
if res != expected {
b.Errorf("got=%s, want=%s", string(res), expected)
}
}
func BenchmarkStringCopy(b *testing.B) {
var res string
for n := 0; n < b.N; n++ {
data := make([]byte, 0, 64) // same size as bootstrap array of bytes.Buffer
for i := 0; i < cnt; i++ {
data = mycopy(data, src)
}
res = string(data)
}
b.StopTimer()
if res != expected {
b.Errorf("got=%s, want=%s", string(res), expected)
}
}
func BenchmarkStringCopy2(b *testing.B) {
var res string
for n := 0; n < b.N; n++ {
b := make([]byte, 0, 64) // same size as bootstrap array of bytes.Buffer
for i := 0; i < cnt; i++ {
l := len(b)
if l+len(src) > cap(b) {
t := make([]byte, 2*cap(b)+len(src))
copy(t, b)
b = t
}
b = b[0 : l+len(src)]
copy(b[l:], src)
}
res = string(b)
}
b.StopTimer()
if res != expected {
b.Errorf("got=%s, want=%s", string(res), expected)
}
}
func BenchmarkStringBuilder(b *testing.B) {
var res string
for n := 0; n < b.N; n++ {
var builder strings.Builder
for i := 0; i < cnt; i++ {
builder.WriteString(src)
}
res = builder.String()
}
b.StopTimer()
if res != expected {
b.Errorf("got=%s, want=%s", string(res), expected)
}
}
|