The garbage collector won't necessarily run between the functions. If you add "runtime.GC()" between functions you'll see that the Array Implementation won't use the heap very much.
There is still some differences, though: https://play.golang.org/p/aYZAzoCzUM4 Map Implementation Latency: 0s Checksum: 499999500000 Heap: 20.578125 MB Stack: 0.1875 MB Array Implementation Latency: 0s Checksum: 499999500000 Heap: 0.1640625 MB Stack: 8.15625 MB Preallocated Slice Implementation Latency: 0s Checksum: 499999500000 Heap: 3.9765625 MB Stack: 4.15625 MB Slice Implementation Latency: 0s Checksum: 499999500000 Heap: 4.6015625 MB Stack: 0.1875 MB Conrado On Thu, Jun 6, 2019 at 7:48 AM Mark Bauermeister <warfan2...@gmail.com> wrote: > Sorry in advance for the somewhat messy code, but this is something I've > been trying to understand all day and can't quite come up with an > explanation for. > Basically, I wrote the below code to experiment with different data types > and their impact on performance and memory efficiency. > > The code itself creates a map, an array, an empty slice and a slice > already preallocated with the 1,000,000 required integers. > > Each data type is being filled with numbers ranging from 0 to 999,999, > then I output the execution time ("latency") and the memory usage ("Heap" > and "Stack"). > > Now here is what I don't quite understand: Why would an array occupy more > space on the heap and stack than a slice? > > Is there an issue in my code or the way I measure heap/stack allocation? > > > > [image: experiment.png] > > > > > > > > > > > > > > > > > > > > > > > > > package main > > import ( > "fmt" > "sync" > "time" > > "runtime" > > "github.com/fatih/color" > ) > > var mu sync.Mutex > var mem runtime.MemStats > > func main() { > imap() > iarray() > ipreSlice() > islice() > } > > func _green(text string) { > fmt.Fprintf(color.Output, color.GreenString(text)) > fmt.Println() > } > > func imap() { > _green("Map Implementation") > cache := make(map[int]int, 1000000) > start := time.Now() > > for i := 0; i < 1000000; i++ { > mu.Lock() > cache[i] = i > mu.Unlock() > } > end := time.Since(start) > fmt.Printf("Latency: %v \n", end) > > var sum int64 > for _, v := range cache { > sum += int64(v) > } > > fmt.Printf("Checksum: %v \n", sum) > printHeap() > printStack() > } > > func iarray() { > _green("Array Implementation") > cache := [1000000]int{} > start := time.Now() > > for i := 0; i < 1000000; i++ { > mu.Lock() > cache[i] = i > mu.Unlock() > } > end := time.Since(start) > fmt.Printf("Latency: %v \n", end) > > var sum int64 > for _, v := range cache { > sum += int64(v) > } > > fmt.Printf("Checksum: %v \n", sum) > printHeap() > printStack() > } > > func ipreSlice() { > _green("Preallocated Slice Implementation") > cache := make([]int, 1000000) > start := time.Now() > > for i := 0; i < 1000000; i++ { > mu.Lock() > cache[i] = i > mu.Unlock() > } > end := time.Since(start) > fmt.Printf("Latency: %v \n", end) > > var sum int64 > for _, v := range cache { > sum += int64(v) > } > > fmt.Printf("Checksum: %v \n", sum) > printHeap() > printStack() > } > > func islice() { > _green("Slice Implementation") > cache := []int{} > start := time.Now() > > for i := 0; i < 1000000; i++ { > mu.Lock() > cache = append(cache, i) > mu.Unlock() > } > end := time.Since(start) > fmt.Printf("Latency: %v \n", end) > > var sum int64 > for _, v := range cache { > sum += int64(v) > } > > fmt.Printf("Checksum: %v \n", sum) > printHeap() > printStack() > } > > func printHeap() { > runtime.ReadMemStats(&mem) > fmt.Printf("Heap: %v MB", float64(mem.HeapInuse)/1024/1024) > fmt.Println() > } > > func printStack() { > runtime.ReadMemStats(&mem) > fmt.Printf("Stack: %v MB", float64(mem.StackInuse)/1024/1024) > fmt.Println() > fmt.Println() > } > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/07bc3bd3-8c9d-48d9-8a36-7e9e42d79637%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/07bc3bd3-8c9d-48d9-8a36-7e9e42d79637%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAHTptW_%3DR96XA%2BRniCtC-Kpo7vXmBPQtT%3DsPQw0ctn85dRbCEA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.