Created
March 13, 2018 01:03
-
-
Save nownabe/5a345da60a092614458ad2c40920586d to your computer and use it in GitHub Desktop.
Go map memory
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 characters
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) | |
}) | |
} |
Author
nownabe
commented
Mar 13, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment