Skip to content

Instantly share code, notes, and snippets.

@0xc0d
Created October 24, 2020 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 0xc0d/525ae05093ca658d181a9e0ac6ecba24 to your computer and use it in GitHub Desktop.
Save 0xc0d/525ae05093ca658d181a9e0ac6ecba24 to your computer and use it in GitHub Desktop.
sync.Pool Benchmark test
type Person struct {
Age int
}
var personPool = sync.Pool{
New: func() interface{} { return new(Person) },
}
func BenchmarkWithoutPool(b *testing.B) {
var p *Person
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 10000; j++ {
p = new(Person)
p.Age = 23
}
}
}
func BenchmarkWithPool(b *testing.B) {
var p *Person
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 10000; j++ {
p = personPool.Get().(*Person)
p.Age = 23
personPool.Put(p)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment