欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Go語言中字符串四種拼接方式的性能對比

 更新時間:2025年04月10日 08:19:04   作者:唐青楓  
在go語言中,字符串是不可變的,因此字符串之間的拼接實際上是創(chuàng)建了一個新的字符串,如果頻繁操作會對性能產(chǎn)生嚴重的影響,下面我們來看看Go語言中字符串四種常見拼接方式的性能對比吧

簡介

使用完整的基準測試代碼文件,可以直接運行來比較四種字符串拼接方法的性能。

  • for 索引 += 的方式
  • for range += 的方式
  • strings.Join 的方式
  • strings.Builder 的方式

寫一個基準測試文件

echo_bench_test.go

package main

import (
	"os"
	"strings"
	"testing"
)

func echoAll1() string {
	var s, sep string
	for i := 0; i < len(os.Args); i++ {
		s += sep + os.Args[i]
		sep = " "
	}
	return s
}

func echoAll2() string {
	s, sep := "", ""
	for _, arg := range os.Args[:] {
		s += sep + arg
		sep = " | "
	}
	return s
}

func echoAll3() string {
	return strings.Join(os.Args[:], " , ")
}

// strings.Builder 是 Go 推薦的高效字符串拼接方式,尤其在循環(huán)中拼接時,
// 可以減少內(nèi)存分配。


func echoAll4() string {
	var builder strings.Builder
	for i, arg := range os.Args[:] {
		if i > 0 {
			builder.WriteString(" <> ")
		}
		builder.WriteString(arg)
	}
	return builder.String()
}


// ===== Benchmark Functions =====

func BenchmarkEchoAll1(b *testing.B) {
	// 模擬更長參數(shù)列表,避免誤差過大
	originalArgs := os.Args
	os.Args = make([]string, 100)
	for i := range os.Args {
		os.Args[i] = "arg"
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = echoAll1()
	}
	os.Args = originalArgs // 恢復
}

func BenchmarkEchoAll2(b *testing.B) {
	originalArgs := os.Args
	os.Args = make([]string, 100)
	for i := range os.Args {
		os.Args[i] = "arg"
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = echoAll2()
	}
	os.Args = originalArgs
}

func BenchmarkEchoAll3(b *testing.B) {
	originalArgs := os.Args
	os.Args = make([]string, 100)
	for i := range os.Args {
		os.Args[i] = "arg"
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = echoAll3()
	}
	os.Args = originalArgs
}

func BenchmarkEchoAll4(b *testing.B) {
	originalArgs := os.Args
	os.Args = make([]string, 100)
	for i := range os.Args {
		os.Args[i] = "arg"
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = echoAll4()
	}
	os.Args = originalArgs
}

運行基準測試

go test -bench=. -benchmem

示例輸出結(jié)果(不同機器會略有不同):

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

每一行含義:

字段含義
BenchmarkEchoAll1測試函數(shù)名
-8使用的 CPU 線程數(shù)(8 核)
500000b.N 的值,代表該函數(shù)跑了 50 萬次
3500 ns/op每次調(diào)用耗時 3500 納秒
120 B/op每次操作分配的字節(jié)數(shù)(字節(jié)越少越好)
5 allocs/op每次操作的內(nèi)存分配次數(shù)(次數(shù)越少越好)

Go 的基準測試自動決定運行次數(shù)(b.N),直到結(jié)果足夠穩(wěn)定。

方法ns/opB/opallocs/op說明
EchoAll13500 ns120 B5+= 每次創(chuàng)建新字符串,開銷大
EchoAll22400 ns104 B4range + +=,仍然多次內(nèi)存分配
EchoAll31600 ns80 B2Join 比較高效
EchoAll4800 ns32 B1strings.Builder 最優(yōu)

到此這篇關(guān)于Go語言中字符串四種拼接方式的性能對比的文章就介紹到這了,更多相關(guān)Go字符串拼接內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Go中過濾范型集合性能示例詳解

    Go中過濾范型集合性能示例詳解

    這篇文章主要為大家介紹了Go中過濾范型集合性能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • 最新評論