Skip to content

Instantly share code, notes, and snippets.

@yu81
Created December 8, 2016 09:27
Show Gist options
  • Save yu81/868767bb0fb45a52aabe0c5b188d66c1 to your computer and use it in GitHub Desktop.
Save yu81/868767bb0fb45a52aabe0c5b188d66c1 to your computer and use it in GitHub Desktop.
package api
import (
"testing"
)
const iteration = 2
func Benchmark_AppendString1(b *testing.B) {
for i := 0; i < b.N; i++ {
appendString1()
}
}
func Benchmark_AppendString2(b *testing.B) {
for i := 0; i < b.N; i++ {
appendString2()
}
}
func Benchmark_AppendString3(b *testing.B) {
for i := 0; i < b.N; i++ {
appendString3()
}
}
func appendString1() {
var list []string
for i := 0; i < iteration; i++ {
list = append(list, "foo")
}
}
func appendString2() {
list := make([]string, 0, iteration)
for i := 0; i < iteration; i++ {
list = append(list, "foo")
}
}
func appendString3() {
list := make([]string, iteration)
for i := 0; i < iteration; i++ {
list[i] = "foo"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment