Skip to content

Instantly share code, notes, and snippets.

@totoleo
Created May 26, 2020 15:41
Show Gist options
  • Save totoleo/ede5fa9a351fdc111512248c7b0cd81d to your computer and use it in GitHub Desktop.
Save totoleo/ede5fa9a351fdc111512248c7b0cd81d to your computer and use it in GitHub Desktop.
func BenchmarkFormat(b *testing.B) {
b.Run("appendFormat", func(b *testing.B) {
b.ReportAllocs()
var specIdStr []byte
var l int = 10
for i := 0; i < b.N; i++ {
val1 := rand.Int63()
val2 := rand.Int63()
val3 := rand.Int63()
var length int = 6*l
specIdStr = make([]byte, 0, length)
specIdStr = strconv.AppendInt(specIdStr, val1, 10)
specIdStr = append(specIdStr, '_')
specIdStr = strconv.AppendInt(specIdStr, val2, 10)
specIdStr = append(specIdStr, '_')
specIdStr = strconv.AppendInt(specIdStr, val3, 10)
if len(specIdStr)>60{
b.Error("larger than 60")
}
}
b.Log(string(specIdStr))
})
b.Run("format", func(b *testing.B) {
b.ReportAllocs()
var specIdStr string
for i := 0; i < b.N; i++ {
val1 := rand.Int63()
val2 := rand.Int63()
val3 := rand.Int63()
specIdStr = strconv.FormatInt(val1, 10)
specIdStr += "_" + strconv.FormatInt(val2, 10)
specIdStr += "_" + strconv.FormatInt(val3, 10)
}
b.Log(specIdStr)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment