Created
November 30, 2023 17:30
-
-
Save avneesh91/f41bd60ea1337c139be33a7bcd58c9ed to your computer and use it in GitHub Desktop.
Revisions
-
avneesh91 created this gist
Nov 30, 2023 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ package main import ( "sync" "testing" ) type MyStruct struct { FirstName string LastName string } func myObjectPool() *sync.Pool { return &sync.Pool{ New: func() interface{} { return &MyStruct{} }, } } func withoutPool() { obj := &MyStruct{} _ = obj } func withPool(pool *sync.Pool) { obj := pool.Get().(*MyStruct) _ = obj pool.Put(obj) } func BenchmarkWithoutPool(b *testing.B) { for i := 0; i < b.N; i++ { withoutPool() } } func BenchmarkWithPool(b *testing.B) { pool := myObjectPool() for i := 0; i < b.N; i++ { withPool(pool) } }