In daily development, every developer faces the task of connecting strings. In Go, we can use+
Operator,、
etc. to splice strings.
Benchmarking
To compare the performance of these methods, I wrote the following benchmark code:
package main import ( "bytes" "strings" "testing" ) func BenchmarkConcat(b *) { var str string for n := 0; n < ; n++ { str += "x" } () } func BenchmarkBuffer(b *) { var buffer for n := 0; n < ; n++ { ("x") } () } func BenchmarkCopy(b *) { bs := make([]byte, ) bl := 0 () for n := 0; n < ; n++ { bl += copy(bs[bl:], "x") } () } func BenchmarkBuilder(b *) { var str for n := 0; n < ; n++ { ("x") } () }
On the command line, enter the directory where the test file is located and execute the command:
go test -bench=Benchmark* -benchmem
Wait for a while and output the test results:
BenchmarkConcat-20 1000000 64276 ns/op 503995 B/op 1 allocs/op
BenchmarkBuffer-20 349817613 3.283 ns/op 3 B/op 0 allocs/op
BenchmarkCopy-20 705887335 1.714 ns/op 0 B/op 0 allocs/op
BenchmarkBuilder-20 637184155 2.125 ns/op 5 B/op 0 allocs/op
`` The above test results show the performance of four connection strings.
+ Operator:
analyze:
- Execution time: 64,276 nanoseconds/op
- Memory allocation: 503,995 bytes/op
- Number of allocations: 1 time/op
- use
+=
The operator performs string splicing, which creates a new string at each iteration, resulting in a larger number of memory allocations and allocations. - Overall, this method has relatively poor performance.
:
analyze:
- Execution time: 3.283 nanoseconds/op
- Memory allocation: 3 bytes/op
- Number of allocations: 0 times/op
- use
Type, this is a good string splicing method.
- It has almost no memory allocation and has very short execution time, making it an efficient way of stitching.
Copy:
analyze:
- Execution time: 1.714 nanoseconds/op
- Memory allocation: 0 bytes/op
- Number of allocations: 0 times/op
- use
copy
Functions that directly copy on preallocated byte arrays. - With the lowest execution time and zero memory allocation, it is the most efficient string splicing method.
:
analyze:
- Execution time: 2.125 nanoseconds/op
- Memory allocation: 5 bytes/op
- Number of allocations: 0 times/op
- use
Type, performance is
and
copy
between. - There are some memory allocations, but it is still an effective method of stitching.
Summarize:
- In this benchmark,
copy
The function performs best, followed byand
, and use
+=
Operators have the worst performance. - When choosing a string splicing method, you should choose an appropriate method according to actual needs and performance requirements to avoid unnecessary memory allocation and improve program execution efficiency.
- use
Taking into account both usage and performance, it is a recommended splicing method.
Test code gobyexample/string-concat/string_concat_test.go at master · denglei1024/gobyexample ()
This is the end of this article about the summary of the method of string splicing in Go. For more related content on Go string splicing, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!