Skip to content

Instantly share code, notes, and snippets.

@awreece
Created May 7, 2012 21:54
Show Gist options
  • Save awreece/2630722 to your computer and use it in GitHub Desktop.
Save awreece/2630722 to your computer and use it in GitHub Desktop.
$ ./command-line-arguments.test --test.bench="Bench"
testing: warning: no tests to run
PASS
BenchmarkConcat 50000 46822 ns/op
BenchmarkJoin 200000 9121 ns/op
BenchmarkBuffer 200000 10764 ns/op
package main
import "bytes"
import "strings"
import "testing"
func BenchmarkConcat(b *testing.B) {
for iter := 0; iter < b.N; iter++ {
s := ""
for i := 0; i < 100; i++ {
s += "AAAA"
}
}
}
func BenchmarkJoin(b *testing.B) {
for iter := 0; iter < b.N; iter++ {
sa := make([]string, 100)
for i := 0; i < 100; i++ {
sa[i] = "AAAA"
}
strings.Join(sa, "")
}
}
func BenchmarkBuffer(b *testing.B) {
for iter := 0; iter < b.N; iter++ {
b := new(bytes.Buffer)
for i := 0; i < 100; i++ {
b.WriteString("AAAA")
}
b.String()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment