Skip to content

Instantly share code, notes, and snippets.

@vkuznecovas vkuznecovas/defer_test.go Secret
Created Jan 16, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.