Skip to content

Instantly share code, notes, and snippets.

@vkuznecovas
Created January 16, 2018 12:50
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 vkuznecovas/66b509cfc24f48b7c60dc7406ff63c11 to your computer and use it in GitHub Desktop.
Save vkuznecovas/66b509cfc24f48b7c60dc7406ff63c11 to your computer and use it in GitHub Desktop.
Simple defer benchmark
package defer_test
import (
"testing"
)
func Calc() {
placeholder:=0
for i := 0; i < 100; i++ {
placeholder += i
}
}
func Bare() {
placeholder:=0
for i := 0; i < 100; i++ {
placeholder += i
}
Calc()
}
func Defer() {
defer Calc()
placeholder:=0
for i := 0; i < 100; i++ {
placeholder += i
}
}
func BenchmarkBare(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 1000; j++ {
Bare()
}
}
}
func BenchmarkDefer(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 1000; j++ {
Defer()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment