Created
January 3, 2016 19:16
-
-
Save nussjustin/2a4996fc19f6e2bcc744 to your computer and use it in GitHub Desktop.
Allocation example
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 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) | |
} | |
} |
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
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