Skip to content

Instantly share code, notes, and snippets.

@maxcnunes
Created March 8, 2019 22:46
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 maxcnunes/2f81933a87555b8ff9219fa6b3822e53 to your computer and use it in GitHub Desktop.
Save maxcnunes/2f81933a87555b8ff9219fa6b3822e53 to your computer and use it in GitHub Desktop.
Performance and memory profiling Go concat vs fmt.Sprintf
go test -bench=. -memprofile=mem0.out -benchmem -benchtime=5s
goos: darwin
goarch: amd64
pkg: github.com/InVisionApp/craft-api/util
BenchmarkHello/concat-8 300000000 19.8 ns/op 0 B/op 0 allocs/op
BenchmarkHello/sprintf-8 50000000 119 ns/op 19 B/op 2 allocs/op
PASS
package test
import (
"fmt"
"testing"
)
func BenchmarkHello(b *testing.B) {
str := "2"
b.Run("concat", func(b *testing.B) {
for i := 0; i < b.N; i++ {
s := str + "xx"
_ = s
}
})
b.Run("sprintf", func(b *testing.B) {
for i := 0; i < b.N; i++ {
s := fmt.Sprintf("%sxx", str)
_ = s
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment