Created
March 8, 2019 22:46
-
-
Save maxclaus/2f81933a87555b8ff9219fa6b3822e53 to your computer and use it in GitHub Desktop.
Performance and memory profiling Go concat vs fmt.Sprintf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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