Skip to content

Instantly share code, notes, and snippets.

@maxcnunes maxcnunes/result
Created Mar 8, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.