最近发布的主题
暂无
最近发布的文章
最近分享的资源
暂无
最近发布的项目
暂无
最近的评论
-
评论了博文 Go 中的垃圾回收:第一部分 - 基础#1楼 @programmersky ...。。。是读着很晦涩么
-
评论了博文 Go: 理解 Sync.Pool 的设计有点尴尬,直接拷的代码,跑了下发现结果不一样 代码 ```golang package pool import ( "runtime" "sync" "testing" ) type Small struct { a int } var pool = sync.Pool{ New: func() interface{} { return new(Small) }, } //go:noinline func inc(s *Small) { s.a++ } func BenchmarkWithoutPool(b *testing.B) { var s *Small for i := 0; i < b.N; i++ { for j := 0; j < 100000; j++ { s = &Small{ a: 1, } b.StopTimer(); inc(s); b.StartTimer() } runtime.GC() } } func BenchmarkWithPool(b *testing.B) { var s *Small for i := 0; i < b.N; i++ { for j := 0; j < 100000; j++ { s = pool.Get().(*Small) s.a = 1 b.StopTimer(); inc(s); b.StartTimer() pool.Put(s) } runtime.GC() } } ``` 结果 ```bash BenchmarkWithoutPool-8 30 42271560 ns/op 1600078 B/op 100000 allocs/op BenchmarkWithPool-8 100 20146449 ns/op 1090 B/op 5 allocs/op ``` 1.12.6的Go,有趣有趣,姿势不正确还是不一定国外文章都对?