亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Go語(yǔ)言中字符串四種拼接方式的性能對(duì)比

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

簡(jiǎn)介

使用完整的基準(zhǔn)測(cè)試代碼文件,可以直接運(yùn)行來(lái)比較四種字符串拼接方法的性能。

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

寫(xiě)一個(gè)基準(zhǔn)測(cè)試文件

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)中拼接時(shí),
// 可以減少內(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) {
	// 模擬更長(zhǎng)參數(shù)列表,避免誤差過(guò)大
	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 // 恢復(fù)
}

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
}

運(yùn)行基準(zhǔn)測(cè)試

go test -bench=. -benchmem

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

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

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

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

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

相關(guān)文章

  • Go中過(guò)濾范型集合性能示例詳解

    Go中過(guò)濾范型集合性能示例詳解

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