Using go1.11 linux/amd64.
I've got this function that logs debugging information:
debugf := func(format string, v ...interface{}) {
if l != nil {
l.Printf(format, v...)
}
}
I was expecting the compiler to inline debugf and avoid consing the varargs when l is nil, but apparently that's not the case:
BenchmarkDebugf/No_Logger-8 30000000 45.4 ns/op 24 B/op 2 allocs/op
BenchmarkDebugf/Logger-8 5000000 291 ns/op 38 B/op 3 allocs/op
Here's a complete test:
package debugf
import (
"log"
"testing"
"io/ioutil"
)
func BenchmarkDebugf(b *testing.B) {
var l *log.Logger
debugf := func(format string, v ...interface{}) {
if l != nil {
l.Printf(format, v...)
}
}
bench := func(b *testing.B) {
for i := 0; i < b.N; i++ {
debugf("i=%v", i)
}
}
b.Run("No Logger", bench)
l = log.New(ioutil.Discard, "", 0)
b.Run("Logger", bench)
}
Using go1.11 linux/amd64.
I've got this function that logs debugging information:
I was expecting the compiler to inline
debugfand avoid consing the varargs whenlis nil, but apparently that's not the case:Here's a complete test: