Skip to content

Instantly share code, notes, and snippets.

@nussjustin
Created January 3, 2016 19:16
Show Gist options
  • Save nussjustin/2a4996fc19f6e2bcc744 to your computer and use it in GitHub Desktop.
Save nussjustin/2a4996fc19f6e2bcc744 to your computer and use it in GitHub Desktop.
Allocation example
package iface_test
import "testing"
func setOneLine(p *int, v interface{}) {
*p = v.(int)
}
func setTwoLines(p *int, v interface{}) {
intv := v.(int)
*p = intv
}
func BenchmarkOneLine(b *testing.B) {
var v int
for i := 0; i < b.N; i++ {
setOneLine(&v, i)
}
}
func BenchmarkTwoLines(b *testing.B) {
var v int
for i := 0; i < b.N; i++ {
setTwoLines(&v, i)
}
}
testing: warning: no tests to run
PASS
BenchmarkOneLine-8 30000000 45.2 ns/op 8 B/op 1 allocs/op
BenchmarkTwoLines-8 100000000 17.2 ns/op 0 B/op 0 allocs/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment