Created
November 30, 2023 17:30
-
-
Save avneesh91/f41bd60ea1337c139be33a7bcd58c9ed to your computer and use it in GitHub Desktop.
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 main | |
import ( | |
"sync" | |
"testing" | |
) | |
type MyStruct struct { | |
FirstName string | |
LastName string | |
} | |
func myObjectPool() *sync.Pool { | |
return &sync.Pool{ | |
New: func() interface{} { | |
return &MyStruct{} | |
}, | |
} | |
} | |
func withoutPool() { | |
obj := &MyStruct{} | |
_ = obj | |
} | |
func withPool(pool *sync.Pool) { | |
obj := pool.Get().(*MyStruct) | |
_ = obj | |
pool.Put(obj) | |
} | |
func BenchmarkWithoutPool(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
withoutPool() | |
} | |
} | |
func BenchmarkWithPool(b *testing.B) { | |
pool := myObjectPool() | |
for i := 0; i < b.N; i++ { | |
withPool(pool) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment