标签:use sync Pool func interface pool
package client import ( "runtime" "sync" "testing" ) type MPool chan interface{} type A struct { s string b int overflow *[2]*[]*string } var p = sync.Pool{ New: func() interface{} { return new(A) }, } var mp MPool = make(chan interface{}, 100) func get() interface{} { select { case r := <-mp: return r default: return new(A) } } func put(a interface{}) { select { case mp <- a: default: } return } func pool() { a := p.Get() p.Put(a) } func init() { runtime.GOMAXPROCS(8) } func BenchmarkName(b *testing.B) { //for i := 0; i < 20; i++ { // p.Put(new(A)) //} //b.ResetTimer() //for i := 0; i < b.N; i++ { // for i := 0; i < 100; i++ { // go func() { // p.Put(p.Get()) // }() // } //} for i := 0; i < 20; i++ { p.Put(new(A)) } b.ResetTimer() for i := 0; i < b.N; i++ { for i := 0; i < 100; i++ { p.Put(p.Get()) } } } func BenchmarkNotPool(b *testing.B) { for i := 0; i < 20; i++ { put(new(A)) } b.ResetTimer() for i := 0; i < b.N; i++ { for i := 0; i < 100; i++ { a := get() put(a) } } }
测试结果:
-----------------------------------------------------------------------
I read sync.Pool
design, but find it is two logic, why we need localPool to solve lock compete. We can just use chan to implement one.
Using channel is 4x times faster than sync.pool
!
Besides pool can clear object, what advantage does it have?
This is the pool implementation and benchmarking code:
package client import ( "runtime" "sync" "testing" ) type MPool chan interface{} type A struct { s string b int overflow *[2]*[]*string } var p = sync.Pool{ New: func() interface{} { return new(A) }, } var mp MPool = make(chan interface{}, 100) func get() interface{} { select { case r := <-mp: return r default: return new(A) } } func put(a interface{}) { select { case mp <- a: default: } return } func pool() { a := p.Get() p.Put(a) } func init() { runtime.GOMAXPROCS(8) } func BenchmarkName(b *testing.B) { for i := 0; i < 20; i++ { p.Put(new(A)) } b.ResetTimer() for i := 0; i < b.N; i++ { for i := 0; i < 100; i++ { go func() { p.Put(p.Get()) }() } } } func BenchmarkNotPool(b *testing.B) { for i := 0; i < 20; i++ { put(new(A)) } b.ResetTimer() for i := 0; i < b.N; i++ { for i := 0; i < 100; i++ { a := get() put(a) } } }
Answer:
You are not benchmarking the same thing, so you can't compare the results.
BenchmarkName()
launches goroutines which have significant overheard and you don't even wait for those goroutines to finish, while BenchmarkNotPool()
just gets and puts an object in the pool in the same goroutine.
If you modify BenchmarkName()
to do the same, the benchmark results actually show it's the other way: sync.Pool
is more than 3 times faster, which is true, so that's its use / advantage.
func BenchmarkName(b *testing.B) { for i := 0; i < 20; i++ { p.Put(new(A)) } b.ResetTimer() for i := 0; i < b.N; i++ { for i := 0; i < 100; i++ { p.Put(p.Get()) } } }
Results:
BenchmarkName-8 500000 2453 ns/op
BenchmarkNotPool-8 200000 7984 ns/op
Also see related question: How to implement Memory Pooling in Golang
Thanks for your great answer, my bad, forgot to do go in BechmarkNotPool, but in adverse we proof use sync.Pool is better. – pete lin Jun 14 '18 at 7:49
- @petelin You can't just launch goroutines in the middle of the benchmark function, and not wait for them to complete. That will render the benchmark result completely useless. – icza Jun 14 '18 at 8:09
标签:use,sync,Pool,func,interface,pool 来源: https://www.cnblogs.com/oxspirt/p/15357921.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。