Skip to content

Instantly share code, notes, and snippets.

@tenntenn
Last active January 10, 2018 02:46
Show Gist options
  • Save tenntenn/c4f65ae90b4038779737ccd5b5de3322 to your computer and use it in GitHub Desktop.
Save tenntenn/c4f65ae90b4038779737ccd5b5de3322 to your computer and use it in GitHub Desktop.
% go version [~/Desktop/go1.10]
go version devel +a99deed39b Mon Jan 8 18:06:27 2018 +0000 darwin/amd64
% go test -bench . -benchmem
goos: darwin
goarch: amd64
BenchmarkStringsBuilderWithoutGrow/10-4 500000 2796 ns/op 160 B/op 5 allocs/op
BenchmarkStringsBuilderWithoutGrow/50-4 200000 6942 ns/op 544 B/op 7 allocs/op
BenchmarkStringsBuilderWithoutGrow/100-4 200000 10813 ns/op 1056 B/op 8 allocs/op
BenchmarkStringsBuilderWithoutGrow/200-4 100000 18351 ns/op 2080 B/op 9 allocs/op
BenchmarkStringsBuilderWithoutGrow/500-4 30000 39791 ns/op 7457 B/op 12 allocs/op
BenchmarkStringsBuilderWithGrow/10-4 1000000 1898 ns/op 32 B/op 1 allocs/op
BenchmarkStringsBuilderWithGrow/50-4 300000 5387 ns/op 32 B/op 1 allocs/op
BenchmarkStringsBuilderWithGrow/100-4 200000 8699 ns/op 32 B/op 1 allocs/op
BenchmarkStringsBuilderWithGrow/200-4 100000 15439 ns/op 32 B/op 1 allocs/op
BenchmarkStringsBuilderWithGrow/500-4 50000 35775 ns/op 32 B/op 1 allocs/op
BenchmarkBytesBufferWithoutGrow/10-4 1000000 2164 ns/op 160 B/op 2 allocs/op
BenchmarkBytesBufferWithoutGrow/50-4 200000 6242 ns/op 752 B/op 4 allocs/op
BenchmarkBytesBufferWithoutGrow/100-4 200000 10146 ns/op 1536 B/op 5 allocs/op
BenchmarkBytesBufferWithoutGrow/200-4 100000 17278 ns/op 3168 B/op 6 allocs/op
BenchmarkBytesBufferWithoutGrow/500-4 30000 39774 ns/op 6625 B/op 7 allocs/op
BenchmarkBytesBufferWithGrow/10-4 1000000 2168 ns/op 160 B/op 2 allocs/op
BenchmarkBytesBufferWithGrow/50-4 200000 5919 ns/op 320 B/op 2 allocs/op
BenchmarkBytesBufferWithGrow/100-4 200000 10095 ns/op 528 B/op 2 allocs/op
BenchmarkBytesBufferWithGrow/200-4 100000 19022 ns/op 1008 B/op 2 allocs/op
BenchmarkBytesBufferWithGrow/500-4 30000 39827 ns/op 2160 B/op 2 allocs/op
PASS
package main
import (
"bytes"
"fmt"
"strings"
"testing"
)
func stringsBuilder(b *testing.B, n int, grow bool) {
for i := 0; i < b.N; i++ {
var builder strings.Builder
b.StopTimer()
if grow {
builder.Grow(n * 4)
}
b.StartTimer()
for j := 0; j < n; j++ {
fmt.Fprint(&builder, "TEST")
}
_ = builder.String()
}
}
func bytesBuffer(b *testing.B, n int, grow bool) {
for i := 0; i < b.N; i++ {
var buf bytes.Buffer
b.StopTimer()
if grow {
buf.Grow(n * 4)
}
b.StartTimer()
for j := 0; j < n; j++ {
fmt.Fprint(&buf, "TEST")
}
_ = buf.String()
}
}
func BenchmarkStringsBuilderWithoutGrow(b *testing.B) {
for _, n := range []int{10, 50, 100, 200, 500} {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
stringsBuilder(b, n, false)
})
}
}
func BenchmarkStringsBuilderWithGrow(b *testing.B) {
for _, n := range []int{10, 50, 100, 200, 500} {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
stringsBuilder(b, n, true)
})
}
}
func BenchmarkBytesBufferWithoutGrow(b *testing.B) {
for _, n := range []int{10, 50, 100, 200, 500} {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
bytesBuffer(b, n, false)
})
}
}
func BenchmarkBytesBufferWithGrow(b *testing.B) {
for _, n := range []int{10, 50, 100, 200, 500} {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
bytesBuffer(b, n, true)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment