Skip to content

Instantly share code, notes, and snippets.

@hxzhouh
Created February 29, 2024 13:04
Show Gist options
  • Save hxzhouh/a07dd587ba52a8afdb07d5655c8ca303 to your computer and use it in GitHub Desktop.
Save hxzhouh/a07dd587ba52a8afdb07d5655c8ca303 to your computer and use it in GitHub Desktop.
defer benchmark test
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