Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"bytes"
"fmt"
"strings"
"testing"
)
func BenchmarkBytesBufferString(b *testing.B) {
for _, n := range []int{10, 100, 1000, 10000, 100000} {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
b.StartTimer()
var buf bytes.Buffer
for i := 0; i < 100; i++ {
buf.WriteString("sample\n")
}
for i := 0; i < n; i++ {
_ = buf.String()
}
}
})
}
}
func BenchmarkStringsBuilderString(b *testing.B) {
for _, n := range []int{10, 100, 1000, 10000, 100000} {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
b.StartTimer()
var buf strings.Builder
for i := 0; i < 100; i++ {
buf.WriteString("sample\n")
}
for i := 0; i < n; i++ {
_ = buf.String()
}
}
})
}
}
func BenchmarkBytesBufferWriteString(b *testing.B) {
for _, n := range []int{10, 100, 1000, 10000, 100000} {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
b.StartTimer()
var buf bytes.Buffer
for i := 0; i < n; i++ {
buf.WriteString("sample\n")
}
_ = buf.String()
}
})
}
}
func BenchmarkStringsBuilderWriteString(b *testing.B) {
for _, n := range []int{10, 100, 1000, 10000, 100000} {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
b.StartTimer()
var buf strings.Builder
for i := 0; i < n; i++ {
buf.WriteString("sample\n")
}
_ = buf.String()
}
})
}
}
func BenchmarkGrownBytesBufferWriteString(b *testing.B) {
for _, n := range []int{10, 100, 1000, 10000, 100000} {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
b.StartTimer()
var buf bytes.Buffer
buf.Grow(n * 10)
for i := 0; i < n; i++ {
buf.WriteString("sample\n")
}
_ = buf.String()
}
})
}
}
func BenchmarkGrownStringsBuilderWriteString(b *testing.B) {
for _, n := range []int{10, 100, 1000, 10000, 100000} {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
b.StartTimer()
var buf strings.Builder
buf.Grow(n * 10)
for i := 0; i < n; i++ {
buf.WriteString("sample\n")
}
_ = buf.String()
}
})
}
}
goos: linux
goarch: amd64
pkg: github.com/knsh14/builder-sample
BenchmarkBytesBufferString/10-2 100000 14764 ns/op 9312 B/op 15 allocs/op
BenchmarkBytesBufferString/100-2 50000 32023 ns/op 72672 B/op 105 allocs/op
BenchmarkBytesBufferString/1000-2 5000 249965 ns/op 706272 B/op 1005 allocs/op
BenchmarkBytesBufferString/10000-2 500 2592241 ns/op 7042272 B/op 10005 allocs/op
BenchmarkBytesBufferString/100000-2 50 26764780 ns/op 70402272 B/op 100005 allocs/op
BenchmarkStringsBuilderString/10-2 200000 10067 ns/op 2048 B/op 8 allocs/op
BenchmarkStringsBuilderString/100-2 200000 10249 ns/op 2048 B/op 8 allocs/op
BenchmarkStringsBuilderString/1000-2 200000 10932 ns/op 2048 B/op 8 allocs/op
BenchmarkStringsBuilderString/10000-2 100000 12917 ns/op 2048 B/op 8 allocs/op
BenchmarkStringsBuilderString/100000-2 20000 60170 ns/op 2048 B/op 8 allocs/op
BenchmarkBytesBufferWriteString/10-2 200000 7928 ns/op 336 B/op 3 allocs/op
BenchmarkBytesBufferWriteString/100-2 100000 10232 ns/op 2976 B/op 6 allocs/op
BenchmarkBytesBufferWriteString/1000-2 100000 23035 ns/op 27104 B/op 9 allocs/op
BenchmarkBytesBufferWriteString/10000-2 10000 142606 ns/op 225760 B/op 12 allocs/op
BenchmarkBytesBufferWriteString/100000-2 1000 2567386 ns/op 3043811 B/op 16 allocs/op
BenchmarkStringsBuilderWriteString/10-2 200000 8992 ns/op 256 B/op 5 allocs/op
BenchmarkStringsBuilderWriteString/100-2 200000 9970 ns/op 2048 B/op 8 allocs/op
BenchmarkStringsBuilderWriteString/1000-2 100000 23022 ns/op 29440 B/op 14 allocs/op
BenchmarkStringsBuilderWriteString/10000-2 10000 198781 ns/op 301056 B/op 22 allocs/op
BenchmarkStringsBuilderWriteString/100000-2 1000 1877717 ns/op 3700736 B/op 32 allocs/op
BenchmarkGrownBytesBufferWriteString/10-2 200000 9197 ns/op 304 B/op 3 allocs/op
BenchmarkGrownBytesBufferWriteString/100-2 200000 9562 ns/op 1840 B/op 3 allocs/op
BenchmarkGrownBytesBufferWriteString/1000-2 100000 18057 ns/op 18544 B/op 3 allocs/op
BenchmarkGrownBytesBufferWriteString/10000-2 10000 127043 ns/op 180336 B/op 3 allocs/op
BenchmarkGrownBytesBufferWriteString/100000-2 2000 1133314 ns/op 1712240 B/op 3 allocs/op
BenchmarkGrownStringsBuilderWriteString/10-2 200000 6626 ns/op 112 B/op 1 allocs/op
BenchmarkGrownStringsBuilderWriteString/100-2 200000 8418 ns/op 1024 B/op 1 allocs/op
BenchmarkGrownStringsBuilderWriteString/1000-2 100000 16621 ns/op 10240 B/op 1 allocs/op
BenchmarkGrownStringsBuilderWriteString/10000-2 10000 113231 ns/op 106496 B/op 1 allocs/op
BenchmarkGrownStringsBuilderWriteString/100000-2 2000 1058665 ns/op 1007616 B/op 1 allocs/op
PASS
ok github.com/knsh14/builder-sample 429.809s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.