佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > golang 字符串拼接性能的對比分析

golang 字符串拼接性能的對比分析

熱門標(biāo)簽:服務(wù)外包 網(wǎng)站排名優(yōu)化 鐵路電話系統(tǒng) AI電銷 呼叫中心市場需求 Linux服務(wù)器 百度競價排名 地方門戶網(wǎng)站

背景

最近在做一個服務(wù)發(fā)現(xiàn)/注冊的agent, 各個服務(wù)需要通過這個agent來注冊自己的服務(wù),在完成

開發(fā)后,測試性能時發(fā)現(xiàn)性能達(dá)不到要求,通過pprof 來確認(rèn)cpu主要耗費(fèi)在gc上,分析結(jié)果主要是由于字符串拼接導(dǎo)致,故需要測試一下字符串拼接的幾種方法的性能;

字符串拼接的幾種方法

1、直接使用加號進(jìn)行拼接

2、strings.Join()

3、fmt.Sprintf()

4、bytes.Buffer

大量字符串拼接性能測試

我們使用的場景主要是大量字符串拼接,所以需要的場景是不斷在字符串上拼接所以測試函數(shù)如下:

// fmt.Printf
func BenchmarkFmtSprintfMore(b *testing.B) {
 var s string
 for i := 0; i  b.N; i++ {
  s += fmt.Sprintf("%s%s", "hello", "world")
 }
 fmt.Errorf(s)
}
// 加號 拼接
func BenchmarkAddMore(b *testing.B) {
 var s string
 for i := 0; i  b.N; i++ {
  s += "hello" + "world"
 }
 fmt.Errorf(s)
}

// strings.Join
func BenchmarkStringsJoinMore(b *testing.B) {

 var s string
 for i := 0; i  b.N; i++ {
  s += strings.Join([]string{"hello", "world"}, "")

 }
 fmt.Errorf(s)
}

// bytes.Buffer
func BenchmarkBufferMore(b *testing.B) {

 buffer := bytes.Buffer{}
 for i := 0; i  b.N; i++ {
  buffer.WriteString("hello")
  buffer.WriteString("world")

 }
 fmt.Errorf(buffer.String())
}

執(zhí)行測試函數(shù)

~/gopath/src/test/string  go test -bench="."
goos: darwin
goarch: amd64
pkg: test/string
BenchmarkFmtSprintfMore-4   300000  118493 ns/op
BenchmarkAddMore-4    300000  124940 ns/op
BenchmarkStringsJoinMore-4  300000  117050 ns/op
BenchmarkBufferMore-4   100000000   37.2 ns/op
PASS
ok  test/string 112.294s

從上可以看出使用bytes.buffer的性能是非常高的,如果涉及到大量數(shù)據(jù)拼接推薦

bytes.buffer{}

單次字符串拼接性能測試

func BenchmarkFmtSprintf(b *testing.B) {
 for i := 0; i  b.N; i++ {
  s := fmt.Sprintf("%s%s", "hello", "world")
  fmt.Errorf(s)
 }

}

func BenchmarkAdd(b *testing.B) {
 for i := 0; i  b.N; i++ {
  s := "hello" + "world"
  fmt.Errorf(s)
 }
}
func BenchmarkStringsJoin(b *testing.B) {
 for i := 0; i  b.N; i++ {
  s := strings.Join([]string{"hello", "world"}, "")
  fmt.Errorf(s)
 }
}
func BenchmarkBuffer(b *testing.B) {

 for i := 0; i  b.N; i++ {
  b := bytes.Buffer{}
  b.WriteString("hello")
  b.WriteString("world")
  fmt.Errorf(b.String())
 }
}

執(zhí)行測試函數(shù)

 ~/gopath/src/test/string  go test -bench="."
goos: darwin
goarch: amd64
pkg: test/string
BenchmarkFmtSprintf-4  10000000   200 ns/op
BenchmarkAdd-4    20000000   93.6 ns/op
BenchmarkStringsJoin-4  10000000   152 ns/op
BenchmarkBuffer-4   10000000   175 ns/op
PASS
ok  test/string 7.818s

從上可以看出單詞調(diào)用字符串拼接性能 + > strings.Join > bytes.Buffer > fmt.Sprintf

總結(jié)

如果涉及到大量數(shù)據(jù)拼接推薦 bytes.buffer{}

后記

當(dāng)然只是通過bytes.buffer{} 性能還是不夠的,針對這個問題我們通過添加緩存進(jìn)一步接口qps.

cpu 耗費(fèi)在gc 上的原因,需要分析golang gc 回收機(jī)制, 這是另外一個topic, 之后總結(jié)。

補(bǔ)充:Go語言字符串批量拼接-StringBuilder

1.安裝

go get -u github.com/typa01/go-utils

import (
 "github.com/typa01/go-utils"
)

使用,例:

fieldNames := tsgutils.NewInterfaceBuilder()

2.使用

func TestStringBuilderReplace(t *testing.T) {
 builder1 := NewStringBuilder()
 builder1.Append("%111%abc%987%")
 FmtPrintln(builder1.Replace("%", "$").ToString()) // $111$abc$987$
 builder2 := builder1.Clear()
 builder2.AppendStrings("abc","defg").AppendInt(123).AppendFloat64(66.44).AppendStrings("aaa", "bbb", "")
 FmtPrintln(builder2.RemoveLast().ToString()) // abcdefg1236.644E+01aaabbb
 str1 := NewString("123")
 builder3 := NewStringBuilderString(str1).Append("456")
 FmtPrintln(builder3.ToString()) // 123456
}

3.GitHub源碼地址

https://github.com/typa01/go-utils

https://github.com/typa01/go-utils/blob/master/string_builder.go

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • 解決golang時間字符串轉(zhuǎn)time.Time的坑
  • Golang 語言高效使用字符串的方法
  • golang中json小談之字符串轉(zhuǎn)浮點數(shù)的操作
  • golang 如何替換掉字符串里面的換行符\n
  • golang 字符串比較是否相等的方法示例
  • 解決Golang json序列化字符串時多了\的情況
  • golang 獲取字符串長度的案例
  • 利用golang的字符串解決leetcode翻轉(zhuǎn)字符串里的單詞
  • 用golang如何替換某個文件中的字符串

標(biāo)簽:蘭州 銅川 黃山 崇左 湘潭 衡水 仙桃 湖南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《golang 字符串拼接性能的對比分析》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    合肥市| 措美县| 巴彦县| 当阳市| 大悟县| 弥勒县| 敖汉旗| 屏东县| 龙山县| 长岭县| 武定县| 大洼县| 类乌齐县| 遵化市| 田林县| 凯里市| 塘沽区| 都昌县| 那坡县| 锦屏县| 长顺县| 玛曲县| 高邑县| 左云县| 沁水县| 德令哈市| 华坪县| 康定县| 文昌市| 永定县| 柳林县| 邻水| 潞城市| 潞西市| 招远市| 中卫市| 丹江口市| 新河县| 陵川县| 抚宁县| 珲春市|