Go Playground
Editor not working?
Switch to
simple mode
.
package main import ("fmt"; "time"; "runtime"; "container/list"; "sync") func main() { numSkipItems := int(1e4) numItems := int(1e4) s32h(numSkipItems, false) runtime.GC() s32h(numItems, true) time.Sleep(5e9) } func s32h(numItems int, out bool) { now, now2, ok, chsz := int64(0), int64(0), true, 2 _ = ok fnOut := func(s string) { if out { fmt.Printf("Op: %+20v: %+12v ns\n", s, now2-now) } } now = time.Nanoseconds() for i := 0; i < numItems; i++ { m := make([]int, 2) m[0] = 1 } now2 = time.Nanoseconds() fnOut("slice") now = time.Nanoseconds() for i := 0; i < numItems; i++ { m := list.New() m.PushFront(1) _ = m } now2 = time.Nanoseconds() fnOut("list") now = time.Nanoseconds() for i := 0; i < numItems; i++ { ch := make(chan int, chsz) close(ch) } now2 = time.Nanoseconds() fnOut("channel-create") now = time.Nanoseconds() for i := 0; i < numItems; i++ { ch := make(chan int, chsz) for j := 0; j < chsz; j++ { ch <- 1 } close(ch) for j := range ch { _ = j } //{ print("ch close: ", j) } // //ch <- 1; <- ch } now2 = time.Nanoseconds() fnOut("channel+send/recv") now = time.Nanoseconds() for i := 0; i < numItems; i++ { m := make(map[int]int, 2) m[1] = 2 _, ok = m[0] } now2 = time.Nanoseconds() fnOut("map") now = time.Nanoseconds() wg := new(sync.WaitGroup) for i := 0; i < numItems; i++ { wg.Add(1) go func(ii int) { ii++; wg.Done() }(i) } wg.Wait() now2 = time.Nanoseconds() fnOut("goroutine") }