以下五個字符串連接函數(shù),你認(rèn)為哪一個最快?
func StrConcat1(strs []string) (string) {
var str string
for _, value := range strs {
str += value
}
return str
}
func StrConcat2(strs []string) (string) {
if len(strs) == 0 {
return ""
}
b := bytes.Buffer{}
for _, s := range strs {
b.WriteString(s)
}
return b.String()
}
func StrConcat3(strs []string) (string) {
return strings.Join(strs, "")
}
func StrConcat4(strs []string) (string) {
var data []byte
for _, value := range strs {
data = append(data, value...)
}
return string(data)
}
func StrConcat5(strs []string) (string) {
var length int
for _, s := range strs {
length += len(s)
}
bs := make([]byte, length)
var i int
for _, value := range strs {
i += copy(bs[i:], value)
}
return string(bs[:])
}
編寫如下的測試文件
import "testing"
var TestStr = []string{"hello", "world", "my", "god", "this", "is", "big", "content"}
func Benchmark_StrConcat1(b *testing.B) {
for i := 0; i < b.N ; i++ {
StrConcat1(TestStr)
}
}
func Benchmark_StrConcat2(b *testing.B) {
for i := 0; i < b.N ; i++ {
StrConcat2(TestStr)
}
}
func Benchmark_StrConcat3(b *testing.B) {
for i := 0; i < b.N ; i++ {
StrConcat3(TestStr)
}
}
func Benchmark_StrConcat4(b *testing.B) {
for i := 0; i < b.N ; i++ {
StrConcat4(TestStr)
}
}
func Benchmark_StrConcat5(b *testing.B) {
for i := 0; i < b.N ; i++ {
StrConcat5(TestStr)
}
}
然后執(zhí)行測試命令
go test -test.bench=".*"
執(zhí)行結(jié)果如下:
Benchmark_StrConcat1-4 5000000 366 ns/op
Benchmark_StrConcat2-4 10000000 178 ns/op
Benchmark_StrConcat3-4 10000000 127 ns/op
Benchmark_StrConcat4-4 10000000 178 ns/op
Benchmark_StrConcat5-4 20000000 102 ns/op
可見,第5個函數(shù)的效率是最高的,雖然它的代碼量最多。
其次是第3個函數(shù),其實我們查看第3個函數(shù)的源代碼就可以發(fā)現(xiàn),它與第5個函數(shù)本質(zhì)上是一樣的,但是因為多了對分隔符的拷貝操作,執(zhí)行時間增加了。我認(rèn)為這個系統(tǒng)函數(shù)可以再優(yōu)化,判斷分隔符是不是空的,如果是空的,就按第5個函數(shù)那個處理,這樣效率還能提升15%。
附,strings.Join函數(shù)源代碼:
// Join concatenates the elements of a to create a single string. The separator string
// sep is placed between elements in the resulting string.
func Join(a []string, sep string) string {
switch len(a) {
case 0:
return ""
case 1:
return a[0]
case 2:
// Special case for common small values.
// Remove if golang.org/issue/6714 is fixed
return a[0] + sep + a[1]
case 3:
// Special case for common small values.
// Remove if golang.org/issue/6714 is fixed
return a[0] + sep + a[1] + sep + a[2]
}
n := len(sep) * (len(a) - 1)
for i := 0; i < len(a); i++ {
n += len(a[i])
}
b := make([]byte, n)
bp := copy(b, a[0])
for _, s := range a[1:] {
bp += copy(b[bp:], sep)
bp += copy(b[bp:], s)
}
return string(b)
}
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。
文章標(biāo)題:尋找golang中最高效的字符串連接函數(shù)-創(chuàng)新互聯(lián)
本文路徑:http://jinyejixie.com/article10/cccpgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站內(nèi)鏈、品牌網(wǎng)站設(shè)計、網(wǎng)站導(dǎo)航、動態(tài)網(wǎng)站、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容