Created
October 24, 2020 18:40
-
-
Save 0xc0d/525ae05093ca658d181a9e0ac6ecba24 to your computer and use it in GitHub Desktop.
sync.Pool Benchmark test
This file contains hidden or 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
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