Skip to content

Instantly share code, notes, and snippets.

@nownabe
Created March 13, 2018 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nownabe/5a345da60a092614458ad2c40920586d to your computer and use it in GitHub Desktop.
Save nownabe/5a345da60a092614458ad2c40920586d to your computer and use it in GitHub Desktop.
Go map memory
package main
import (
"fmt"
"runtime"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
var p = message.NewPrinter(language.English)
func profile(f func() string) {
var mem runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&mem)
before := mem.Alloc
title := f()
runtime.ReadMemStats(&mem)
after := mem.Alloc
p.Printf("%s:%d bytes\n", title, after-before)
}
func main() {
var i, j int32
numUsers := int32(100000)
profile(func() string {
v := map[int32]int32{}
for i = 0; i < numUsers; i++ {
v[i] = i
}
return fmt.Sprintf("%T", v)
})
profile(func() string {
v := map[uint16]uint16{}
for i = 0; i < numUsers; i++ {
v[uint16(i)] = uint16(i)
}
return fmt.Sprintf("%T", v)
})
numUsers = int32(10000)
max := int32(5000)
profile(func() string {
v := map[int32]map[int32]float32{}
for i = 0; i < numUsers; i++ {
v[i] = map[int32]float32{}
for j = 0; j < max; j++ {
v[i][j] = 0.0
}
}
return fmt.Sprintf("%T", v)
})
profile(func() string {
v := map[uint16]map[uint16]uint8{}
for i = 0; i < numUsers; i++ {
v[uint16(i)] = map[uint16]uint8{}
for j = 0; j < max; j++ {
// Quantization
v[uint16(i)][uint16(j)] = uint8(0.0 * 255)
}
}
return fmt.Sprintf("%T", v)
})
}
@nownabe
Copy link
Author

nownabe commented Mar 13, 2018

map[int32]int32:3,269,192 bytes
map[uint16]uint16:1,867,928 bytes
map[int32]map[int32]float32:1,125,422,336 bytes
map[uint16]map[uint16]uint8:664,942,024 bytes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment