Skip to content

Instantly share code, notes, and snippets.

@jcoene
Created February 10, 2016 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcoene/47841276b8c3e8e49b2b to your computer and use it in GitHub Desktop.
Save jcoene/47841276b8c3e8e49b2b to your computer and use it in GitHub Desktop.
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)
}
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