-
-
Save vkuznecovas/66b509cfc24f48b7c60dc7406ff63c11 to your computer and use it in GitHub Desktop.
Simple defer benchmark
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_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