Created
February 29, 2024 13:04
-
-
Save hxzhouh/a07dd587ba52a8afdb07d5655c8ca303 to your computer and use it in GitHub Desktop.
defer benchmark test
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
func sum(max int) int { | |
total := 0 | |
for i := 0; i < max; i++ { | |
total += i | |
} | |
return total | |
} | |
func fooWithDefer() { defer func() { sum(10) }() } | |
func fooWithoutDefer() { sum(10) } | |
func BenchmarkFooWithDefer(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
fooWithDefer() | |
} | |
} | |
func BenchmarkFooWithoutDefer(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
fooWithoutDefer() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment