GolangNote

Golang笔记

Golang string 和byte 操作性能比较

Permalink

Golang string 和 byte 操作性能比较,是选择哪种类型作业务运算,这个比较可能对你有帮助。

先上结论:

plaintext: 结论
1
2
Strings are faster for searches (contains, index, compare) purpose.
bytes are faster in create (replace, concat) purpose.

  • strings 在做搜索🔍 (包含、索引、比较)时速度稍优于 bytes
  • bytes 在做创建(替换、连接)时速度稍优于 strings

下面代码

Go:
1
2
value, ok := mymap[string(mybytes)]
BenchmarkMapHints-4 100000000 12.3 ns/op 0 B/op 0 allocs/op

优于

Go:
1
2
3
key := string(mybytes)
value, ok := mymap[key]
BenchmarkMapsHints_Dont-4 100000000 22.0 ns/op 0 B/op 0 allocs/op

byte to string 的开销

Go: byte to string 的开销
1
2
b:= string(bytes)
BenchmarkBytesToStrings-4  30000000  38.6 ns/op 32 B/op 1 allocs/op

测试代码

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main

import (
	"bytes"
	"strings"
	"testing"
)

var result interface{}

//BenchmarkBytesToStrings convert a []bytes in a string
//https://golang.org/pkg/bytes/#Compare
func BenchmarkBytesToStrings(b *testing.B) {

	s1 := []byte("string to convert")

	b.ResetTimer()
	b.ReportAllocs()
	var r string
	for n := 0; n < b.N; n++ {
		r = string(s1)
	}

	result = r
}

//BenchmarkBytesCompare compare 2 []bytes.
//https://golang.org/pkg/bytes/#Compare
func BenchmarkBytesCompare(b *testing.B) {

	s1 := []byte("string to compare")
	s2 := []byte("string to compare")

	b.ResetTimer()
	b.ReportAllocs()
	var r int
	for n := 0; n < b.N; n++ {
		r = bytes.Compare(s1, s2)
	}

	result = r
}

//BenchmarkStringsCompare compare 2 strings.
//https://golang.org/pkg/strings/#Compare
func BenchmarkStringsCompare(b *testing.B) {

	s1 := "string to compare"
	s2 := "string to compare"

	b.ResetTimer()
	b.ReportAllocs()
	var r int
	for n := 0; n < b.N; n++ {
		r = strings.Compare(s1, s2)
	}

	result = r
}

//BenchmarkBytesContains check contains method
//https://golang.org/pkg/bytes/#Contains
func BenchmarkBytesContains(b *testing.B) {

	s1 := []byte("string to compare")
	s2 := []byte("comparc")

	b.ResetTimer()
	b.ReportAllocs()
	var r bool
	for n := 0; n < b.N; n++ {
		r = bytes.Contains(s1, s2)
	}

	result = r
}

//BenchmarkStringsContains check contains method
//https://golang.org/pkg/strings/#Contains
func BenchmarkStringsContains(b *testing.B) {

	s1 := "string to compare"
	s2 := "comparc"

	b.ResetTimer()
	b.ReportAllocs()
	var r bool
	for n := 0; n < b.N; n++ {
		r = strings.Contains(s1, s2)
	}

	result = r
}

//BenchmarkBytesIndex check contains index
//https://golang.org/pkg/bytes/#Index
func BenchmarkBytesIndex(b *testing.B) {

	s1 := []byte("string to compare")
	s2 := []byte("e")

	b.ResetTimer()
	b.ReportAllocs()
	var r int
	for n := 0; n < b.N; n++ {
		r = bytes.Index(s1, s2)
	}

	result = r
}

//BenchmarkStringIndex check contains index
//https://golang.org/pkg/strings/#Index
func BenchmarkStringIndex(b *testing.B) {

	s1 := "string to compare"
	s2 := "e"

	b.ResetTimer()
	b.ReportAllocs()
	var r int
	for n := 0; n < b.N; n++ {
		r = strings.Index(s1, s2)
	}

	result = r
}

//BenchmarkBytesReplace check replace method
//https://golang.org/pkg/bytes/#Replace
func BenchmarkBytesReplace(b *testing.B) {

	s1 := []byte("string to comparc")
	s2 := []byte("comparc")
	s3 := []byte("compare")

	b.ResetTimer()
	b.ReportAllocs()
	var r []byte
	for n := 0; n < b.N; n++ {
		r = bytes.Replace(s1, s2, s3, -1)
	}

	result = r
}

//BenchmarkStringsReplace check replace method
//https://golang.org/pkg/strings/#Replace
func BenchmarkStringsReplace(b *testing.B) {

	s1 := "string to comparc"
	s2 := "comparc"
	s3 := "compare"

	b.ResetTimer()
	b.ReportAllocs()
	var r string
	for n := 0; n < b.N; n++ {
		r = strings.Replace(s1, s2, s3, -1)
	}

	result = r
}

//BenchmarkBytesConcat concats 2 bytes
func BenchmarkBytesConcat2(b *testing.B) {

	s1 := []byte("string to compare")
	s2 := []byte("string to add")
	b.ResetTimer()
	b.ReportAllocs()

	for n := 0; n < b.N; n++ {
		r := make([]byte, 0, len(s1)+len(s2))
		r = append(r, s1...)
		r = append(r, s2...)
	}
}

//BenchmarkStringsConcat concats 2 strings
func BenchmarkStringsConcat2(b *testing.B) {

	s1 := "string to compare"
	s2 := "string to add"
	b.ResetTimer()
	b.ReportAllocs()
	var r string
	for n := 0; n < b.N; n++ {
		r = s1 + s2
	}

	result = r
}

//BenchmarkStringsJoin joins 2 strings
//https://golang.org/pkg/strings/#Join
func BenchmarkStringsJoin2(b *testing.B) {

	s1 := "string to compare"
	s2 := "string to add"
	b.ResetTimer()
	b.ReportAllocs()
	var r string
	for n := 0; n < b.N; n++ {
		j := []string{s1, s2}
		r = strings.Join(j, "")
	}

	result = r
}

//BenchmarkMapHints bench mymap[string(abytes)]
func BenchmarkMapHints(b *testing.B) {
	mymap := make(map[string]string)
	mymap["key"] = "value"
	abytes := []byte("key")

	b.ResetTimer()
	b.ReportAllocs()

	var v string
	for n := 0; n < b.N; n++ {
		v, _ = mymap[string(abytes)]
	}

	result = v
}

//BenchmarkMapsHints_Dont bench key := string(abytes)
//v, _ = mymap[key]
func BenchmarkMapsHints_Dont(b *testing.B) {
	mymap := make(map[string]string)
	mymap["key"] = "value"
	abytes := []byte("key")

	b.ResetTimer()
	b.ReportAllocs()

	var v string
	for n := 0; n < b.N; n++ {
		key := string(abytes)
		v, _ = mymap[key]
	}

	result = v

}

本文网址: https://golangnote.com/topic/187.html 转摘请注明来源

Related articles

Golang 泛型性能初识

编程时,我们通常需要编写“泛型”函数,其中确切的数据类型并不重要。例如,您可能想编写一个简单的函数来对数字进行求和。Go 直到最近才有这个概念,但最近才添加了它(从1.18版开始)。...

Golang Range 的性能提升Tip

Go 语言里使用 range 可以方便遍历数组(array)、切片(slice)、字典(map)和信道(chan)。这里主要关注他们的性能。...

Write a Comment to "Golang string 和byte 操作性能比较"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 1ms