string 中的一些优化事项
1.1 fmt vs "+" (无转义)
import ( "testing" "fmt" ) var ( str = "hello gohpers!" sep = "," ) func BenchmarkFmt(b *testing.B) { for i := 0; i < b.N; i++ { _ = fmt.Sprint("%s%s%s%s%s", str, sep, str, sep, str) } } func BenchmarkPlus(b *testing.B) { for i := 0; i < b.N; i++ { _ = str + sep + str + sep + str } }
运行结果概括如下:
BenchmarkFmt 3000000 490 ns/op BenchmarkPlus 15000000 78 ns/op
1.1 fmt vs "+" (带有转义)
2. strings.join VS "+"
持续更新中