Introduction
Using the complete benchmark code file, you can run directly to compare the performance of the four string splicing methods.
-
for
index+=
Way -
for range +=
Way -
Way
-
Way
Write a benchmark file
echo_bench_test.go
package main import ( "os" "strings" "testing" ) func echoAll1() string { var s, sep string for i := 0; i < len(); i++ { s += sep + [i] sep = " " } return s } func echoAll2() string { s, sep := "", "" for _, arg := range [:] { s += sep + arg sep = " | " } return s } func echoAll3() string { return ([:], " , ") } // It is the efficient string splicing method recommended by Go, especially when splicing in loops.// Can reduce memory allocation. func echoAll4() string { var builder for i, arg := range [:] { if i > 0 { (" <> ") } (arg) } return () } // ===== Benchmark Functions ===== func BenchmarkEchoAll1(b *) { // Simulate longer parameter lists to avoid excessive errors originalArgs := = make([]string, 100) for i := range { [i] = "arg" } () for i := 0; i < ; i++ { _ = echoAll1() } = originalArgs // recover} func BenchmarkEchoAll2(b *) { originalArgs := = make([]string, 100) for i := range { [i] = "arg" } () for i := 0; i < ; i++ { _ = echoAll2() } = originalArgs } func BenchmarkEchoAll3(b *) { originalArgs := = make([]string, 100) for i := range { [i] = "arg" } () for i := 0; i < ; i++ { _ = echoAll3() } = originalArgs } func BenchmarkEchoAll4(b *) { originalArgs := = make([]string, 100) for i := range { [i] = "arg" } () for i := 0; i < ; i++ { _ = echoAll4() } = originalArgs }
Run the benchmark
go test -bench=. -benchmem
Sample output results (different machines will be slightly different):
goos: darwin
goarch: amd64
pkg: example
BenchmarkEchoAll1-8 500000 3500 ns/op 120 B/op 5 allocs/op
BenchmarkEchoAll2-8 700000 2400 ns/op 104 B/op 4 allocs/op
BenchmarkEchoAll3-8 1000000 1600 ns/op 80 B/op 2 allocs/op
BenchmarkEchoAll4-8 2000000 800 ns/op 32 B/op 1 allocs/op
PASS
ok example 3.456s
Each line means:
Fields | meaning |
---|---|
BenchmarkEchoAll1 | Test function name |
-8 | Number of CPU threads used (8 cores) |
500000 | The value of the function has run 500,000 times |
3500 ns/op | Each call takes 3500 nanoseconds |
120 B/op | The number of bytes allocated for each operation (the fewer bytes, the better) |
5 allocs/op | The number of memory allocations per operation (the fewer times, the better) |
Go
The benchmark test automatically determines the number of runs () until the results are stable enough.
method | ns/op | B/op | allocs/op | illustrate |
---|---|---|---|---|
EchoAll1 | 3500 ns | 120 B | 5 | += Every time you create a new string, the overhead is high |
EchoAll2 | 2400 ns | 104 B | 4 | range + +=, still multiple memory allocations |
EchoAll3 | 1600 ns | 80 B | 2 | Join is more efficient |
EchoAll4 | 800 ns | 32 B | 1 | Optimal |
This is the article about the performance comparison of the four string splicing methods in Go. For more related Go string splicing content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!