Golang字符串拼接效率
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | package main import ( "bytes" "strings" "testing" ) var strLen int = 1000 func BenchmarkConcatString(b *testing.B) { var str string i := 0 b.ResetTimer() for n :=0; n< b.N; n++ { str += "X" i++ if i >= strLen { i = 0 str = "" } } } func BenchmarkConcatBuffer(b *testing.B) { var buffer bytes.Buffer i := 0 b.ResetTimer() for n := 0; n < b.N; n++ { buffer.WriteString( "X" ) i++ if i >= strLen { i = 0 buffer = bytes.Buffer{} } } } func BenchmarkConcatBuilder(b *testing.B) { var builder strings.Builder i := 0 b.ResetTimer() for n := 0; i < b.N; n++ { builder.WriteString( "X" ) i++ if i > strLen { i = 0 builder = strings.Builder{} } } } |
bash: go test -bench=. -test.benchmem goos: darwin goarch: amd64 pkg: benchmark BenchmarkConcatString-4 10000000 169 ns/op 530 B/op 0 allocs/op BenchmarkConcatBuffer-4 100000000 11.7 ns/op 2 B/op 0 allocs/op BenchmarkConcatBuilder-4 100000000 13.1 ns/op 2 B/op 0 allocs/op PASS ok benchmark 4.416s
help:
-test.bench regexp run only benchmarks matching regexp -test.benchmem print memory allocations for benchmarks -test.benchtime d run each benchmark for duration d (default 1s) -test.blockprofile file write a goroutine blocking profile to file -test.blockprofilerate rate set blocking profile rate (see runtime.SetBlockProfileRate) (default 1) -test.count n run tests and benchmarks n times (default 1) -test.coverprofile file write a coverage profile to file -test.cpu list comma-separated list of cpu counts to run each test with -test.cpuprofile file write a cpu profile to file -test.failfast do not start new tests after the first test failure -test.list regexp list tests, examples, and benchmarks matching regexp then exit -test.memprofile file write a memory profile to file -test.memprofilerate rate set memory profiling rate (see runtime.MemProfileRate) -test.mutexprofile string write a mutex contention profile to the named file after execution -test.mutexprofilefraction int if >= 0, calls runtime.SetMutexProfileFraction() (default 1) -test.outputdir dir write profiles to dir -test.parallel n run at most n tests in parallel (default 4) -test.run regexp run only tests and examples matching regexp -test.short run smaller test suite to save time -test.testlogfile file write test action log to file (for use only by cmd/go) -test.timeout d panic test binary after duration d (default 0, timeout disabled) -test.trace file write an execution trace to file -test.v verbose: print additional output
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 后端思维之高并发处理方案
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析