Closed
Description
type BoolStruct struct {
b1 bool
b2 bool
b3 bool
b4 bool
b5 bool
b6 bool
b7 bool
b8 bool
b byte
}
func (bs *BoolStruct) DoSomething() {
bs.b = DoSomethingForBool(bs.b1, bs.b2, bs.b3, bs.b4, bs.b5, bs.b6, bs.b7, bs.b8)
}
func DoSomethingForBool(b1 bool, b2 bool, b3 bool, b4 bool, b5 bool, b6 bool, b7 bool, b8 bool) byte {
var b byte
if b1 {
//do something
b++
}
if b2 {
//do something
b++
}
if b3 {
//do something
b++
}
if b4 {
//do something
b++
}
if b5 {
//do something
b++
}
if b6 {
//do something
b++
}
if b7 {
//do something
b++
}
if b8 {
//do something
b++
}
return b
}
var bsFalse = BoolStruct{
b1: false,
b2: false,
b3: false,
b4: false,
b5: false,
b6: false,
b7: false,
b8: false,
}
func BenchmarkBSFalse(b *testing.B) {
for i := 0; i < b.N; i++ {
bsFalse.DoSomething()
}
}
var bsTrue = BoolStruct{
b1: true,
b2: true,
b3: true,
b4: true,
b5: true,
b6: true,
b7: true,
b8: true,
}
func BenchmarkBSTrue(b *testing.B) {
for i := 0; i < b.N; i++ {
bsTrue.DoSomething()
}
}
BenchmarkBSFalse-6 502713746 7.17 ns/op 0 B/op 0 allocs/op
BenchmarkBSTrue-6 1000000000 2.17 ns/op 0 B/op 0 allocs/op
all false consume more time than all true, why?