Skip to content

Instantly share code, notes, and snippets.

@ukautz
Last active August 26, 2019 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukautz/4b9340da2df1eda74d876c510c7e523c to your computer and use it in GitHub Desktop.
Save ukautz/4b9340da2df1eda74d876c510c7e523c to your computer and use it in GitHub Desktop.
$ for i in seq 1 3; do go test -bench=. -benchtime=5s; echo; done
goos: linux
goarch: amd64
pkg: github.com/Scout24/go-observability/.local
BenchmarkCallWithoutSlice-4    	100000000	        76.8 ns/op
BenchmarkCallWithSlice-4       	100000000	        77.1 ns/op
BenchmarkCallWithSliceCopy-4   	100000000	        76.4 ns/op
PASS
ok  	github.com/Scout24/go-observability/.local	23.277s

goos: linux
goarch: amd64
pkg: github.com/Scout24/go-observability/.local
BenchmarkCallWithoutSlice-4    	100000000	        77.0 ns/op
BenchmarkCallWithSlice-4       	100000000	        77.6 ns/op
BenchmarkCallWithSliceCopy-4   	100000000	        76.8 ns/op
PASS
ok  	github.com/Scout24/go-observability/.local	23.406s

goos: linux
goarch: amd64
pkg: github.com/Scout24/go-observability/.local
BenchmarkCallWithoutSlice-4    	100000000	        78.3 ns/op
BenchmarkCallWithSlice-4       	100000000	        80.6 ns/op
BenchmarkCallWithSliceCopy-4   	100000000	        83.3 ns/op
PASS
ok  	github.com/Scout24/go-observability/.local	24.502s
package _local
import (
"strings"
"testing"
)
var globalSlice = []string{"a", "b", "c"}
func callWithoutSlice(a string) string {
return a + ":" + strings.Join(globalSlice, ":")
}
func callWithSlice(a string, in []string) string {
return a + ":" + strings.Join(in, ":")
}
func BenchmarkCallWithoutSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
callWithoutSlice("X")
}
}
func BenchmarkCallWithSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
callWithSlice("X", []string{"a", "b", "c"})
}
}
func BenchmarkCallWithSliceCopy(b *testing.B) {
from := []string{"a", "b", "c"}
for i := 0; i < b.N; i++ {
callWithSlice("X", from[:])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment