Golang字串拼接效率

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
package mainimport ("bytes""strings""testing")var strLen int = 1000func BenchmarkConcatString(b *testing.B) {var str stringi := 0b.ResetTimer()for n :=0; n< b.N; n++ {str += "X"i++if i >= strLen {i = 0str = ""}}}func BenchmarkConcatBuffer(b *testing.B) {var buffer bytes.Bufferi := 0b.ResetTimer()for n := 0; n < b.N; n++ {buffer.WriteString("X")i++if i >= strLen {i = 0buffer = bytes.Buffer{}}}}func BenchmarkConcatBuilder(b *testing.B) {var builder strings.Builderi := 0b.ResetTimer()for n := 0; i < b.N; n++ {builder.WriteString("X")i++if i > strLen {i = 0builder = strings.Builder{}}}} 

 

bash: go test -bench=. -test.benchmemgoos: darwingoarch: amd64pkg: benchmarkBenchmarkConcatString-4        10000000           169 ns/op         530 B/op           0 allocs/opBenchmarkConcatBuffer-4        100000000            11.7 ns/op           2 B/op           0 allocs/opBenchmarkConcatBuilder-4       100000000            13.1 ns/op           2 B/op           0 allocs/opPASSok      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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.