Goでのベンチマークの取り方。
https://golang.org/pkg/testing/
package main
import (
"fmt"
"testing"
)
func makeSlice1(n int) []string {
var r []string
for i := 0; i < n; i++ {
r = append(r, fmt.Sprintf("%03d hello", i))
}
return r
}
func makeSlice2(n int) []string {
r := make([]string, n)
for i := 0; i < n; i++ {
r[i] = fmt.Sprintf("%03d hello", i)
}
return r
}
func BenchmarkMakeSlice1(b *testing.B) {
b.ResetTimer()
makeSlice1(b.N)
}
func BenchmarkMakeSlice2(b *testing.B) {
b.ResetTimer()
makeSlice2(b.N)
}
$ go test -bench .
goos: darwin
goarch: amd64
BenchmarkMakeSlice1-4 5000000 285 ns/op
BenchmarkMakeSlice2-4 10000000 144 ns/op
PASS
ok _/go/to/path 3.383s