-
-
Save djoreilly/074fe4e4fabece4321158debe71dcd7d to your computer and use it in GitHub Desktop.
sync.Pool 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
// go test -bench=. | |
package main | |
import ( | |
"sync" | |
"testing" | |
) | |
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