Created
February 10, 2016 14:55
-
-
Save jcoene/47841276b8c3e8e49b2b to your computer and use it in GitHub Desktop.
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 benchcmpvsnil | |
import ( | |
"testing" | |
) | |
func BenchmarkNilPtrNil(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
isNilPtr(nil) | |
} | |
} | |
func BenchmarkNilPtrVal(b *testing.B) { | |
x := int32(3) | |
for i := 0; i < b.N; i++ { | |
isNilPtr(&x) | |
} | |
} | |
func isNilPtr(p *int32) bool { | |
return (p != nil) | |
} | |
func BenchmarkZeroCmpZero(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
isZeroVal(0) | |
} | |
} | |
func BenchmarkZeroCmpVal(b *testing.B) { | |
x := int32(3) | |
for i := 0; i < b.N; i++ { | |
isZeroVal(x) | |
} | |
} | |
func isZeroVal(n int32) bool { | |
return (n > 0) | |
} |
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
PASS | |
BenchmarkNilPtrNil-4 2000000000 0.73 ns/op 0 B/op 0 allocs/op | |
BenchmarkNilPtrVal-4 2000000000 0.65 ns/op 0 B/op 0 allocs/op | |
BenchmarkZeroCmpZero-4 2000000000 0.54 ns/op 0 B/op 0 allocs/op | |
BenchmarkZeroCmpVal-4 2000000000 0.47 ns/op 0 B/op 0 allocs/op | |
ok github.com/dotabuff/benchcmpvsnil 5.008s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment