Skip to content

Instantly share code, notes, and snippets.

@ken39arg
Created June 24, 2020 05:02
Show Gist options
  • Select an option

  • Save ken39arg/7c0edba919df00230fc58f433c2cec52 to your computer and use it in GitHub Desktop.

Select an option

Save ken39arg/7c0edba919df00230fc58f433c2cec52 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strconv"
"testing"
)
var columns = []string{"a", "b", "c", "d", "e", "f"}
func Benchmark_AddStr(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
nc := make([]string, len(columns))
for j, c := range columns {
nc[j] = "c." + c
}
}
}
func Benchmark_SprintfStr(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
nc := make([]string, len(columns))
for j, c := range columns {
nc[j] = fmt.Sprintf("c.%s", c)
}
}
}
func Benchmark_AddInt(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
nc := make([]string, len(columns))
for j := range columns {
nc[j] = "c." + strconv.Itoa(j)
}
}
}
func Benchmark_SprintfInt(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
nc := make([]string, len(columns))
for j := range columns {
nc[j] = fmt.Sprintf("c.%d", j)
}
}
}
@ken39arg

Copy link
Copy Markdown
Author
# go test -bench . -benchmem
goos: linux
goarch: amd64
pkg: github.com/***
Benchmark_AddStr-2               2648538               635 ns/op             115 B/op          7 allocs/op
Benchmark_SprintfStr-2           1000000              1584 ns/op             211 B/op         13 allocs/op
Benchmark_AddInt-2               2533928               491 ns/op             115 B/op          7 allocs/op
Benchmark_SprintfInt-2            636294              1864 ns/op             176 B/op         12 allocs/op
PASS
ok      github.com/***    7.855s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment