Golang字符串拼接效率

jachin · · 3099 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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

 


有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:jachin

查看原文:Golang字符串拼接效率

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

3099 次点击  
加入收藏 微博
下一篇:Golang测试包
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传