Last active
August 29, 2015 14:12
-
-
Save ttacon/f4512a3b8d132786595a to your computer and use it in GitHub Desktop.
Fastest way to make a *byte.Buffer from a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"math/rand" | |
"testing" | |
) | |
// go test -v -bench=. -benchtime=10s | |
// | |
// Benchmark_NewBuffer20 1000000000 16.8 ns/op 0 B/op 0 allocs/op | |
// Benchmark_NewBuffer1000 1000000000 16.3 ns/op 0 B/op 0 allocs/op | |
// Benchmark_NewString20 100000000 157 ns/op 32 B/op 1 allocs/op | |
// Benchmark_NewString1000 10000000 2161 ns/op 1024 B/op 1 allocs/op | |
// Benchmark_Write20 50000000 344 ns/op 112 B/op 1 allocs/op | |
// Benchmark_Write1000 5000000 3106 ns/op 1136 B/op 2 allocs/op | |
// Benchmark_WriteString20 50000000 346 ns/op 112 B/op 1 allocs/op | |
// Benchmark_WriteString1000 5000000 3049 ns/op 1136 B/op 2 allocs/op | |
var seq20, seq1000 string | |
func init() { | |
seq20 = randSeq(20) | |
seq1000 = randSeq(1000) | |
} | |
func Benchmark_NewBuffer20(b *testing.B) { | |
data := []byte(seq20) | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
_ = bytes.NewBuffer(data) | |
} | |
} | |
func Benchmark_NewBuffer1000(b *testing.B) { | |
data := []byte(seq1000) | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
_ = bytes.NewBuffer(data) | |
} | |
} | |
func Benchmark_NewString20(b *testing.B) { | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
_ = bytes.NewBufferString(seq20) | |
} | |
} | |
func Benchmark_NewString1000(b *testing.B) { | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
_ = bytes.NewBufferString(seq1000) | |
} | |
} | |
func Benchmark_Write20(b *testing.B) { | |
data := []byte(seq20) | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
_, _ = bytes.NewBuffer(nil).Write(data) | |
} | |
} | |
func Benchmark_Write1000(b *testing.B) { | |
data := []byte(seq1000) | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
_, _ = bytes.NewBuffer(nil).Write(data) | |
} | |
} | |
func Benchmark_WriteString20(b *testing.B) { | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
_, _ = bytes.NewBuffer(nil).WriteString(seq20) | |
} | |
} | |
func Benchmark_WriteString1000(b *testing.B) { | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
_, _ = bytes.NewBuffer(nil).WriteString(seq1000) | |
} | |
} | |
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
func randSeq(n int) string { | |
b := make([]rune, n) | |
for i := range b { | |
b[i] = letters[rand.Intn(len(letters))] | |
} | |
return string(b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment