Last active
September 10, 2019 02:20
-
-
Save jiacai2050/f89dff0e24cbf95b4490c1dce6af74c1 to your computer and use it in GitHub Desktop.
go defer benchmark 1.12 vs 1.13
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 defer_demo | |
import ( | |
"testing" | |
) | |
func doDefer() int { | |
acc := 0 | |
for i := 0; i < 100; i++ { | |
defer func(i int) { | |
acc += i | |
}(i) | |
} | |
return acc | |
} | |
func noDefer() int { | |
acc := 0 | |
for i := 0; i < 100; i++ { | |
func(i int) { | |
acc += i | |
}(i) | |
} | |
return acc | |
} | |
var blackhole int | |
func BenchmarkDefer(b *testing.B) { | |
s := 1 | |
for i := 0; i < b.N; i++ { | |
s = doDefer() | |
} | |
blackhole = s | |
} | |
var result int | |
func BenchmarkNoDefer(b *testing.B) { | |
s := 1 | |
for i := 0; i < b.N; i++ { | |
s = noDefer() | |
} | |
result = s | |
} |
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
# go test -v -bench=. -benchmem | |
go version go1.13 darwin/amd64 | |
BenchmarkDefer-8 255255 4291 ns/op 8 B/op 1 allocs/op | |
BenchmarkNoDefer-8 9560637 123 ns/op 0 B/op 0 allocs/op | |
go version go1.12.1 darwin/amd64 | |
BenchmarkDefer-8 300000 4340 ns/op 8 B/op 1 allocs/op | |
BenchmarkNoDefer-8 10000000 125 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
1.12
1.13