Skip to content

Instantly share code, notes, and snippets.

@carbocation
Last active September 4, 2016 02:51
Show Gist options
  • Save carbocation/0528ec1d1552ae7614549cc5cd39048b to your computer and use it in GitHub Desktop.
Save carbocation/0528ec1d1552ae7614549cc5cd39048b to your computer and use it in GitHub Desktop.
Demonstrates that the string representation of an int64 requires twice the memory
package typesizes
import (
"math"
"strconv"
"testing"
)
var N int = 1e7
var stringSize int = 1 + int(math.Log10(float64(N)))
func TestStringAndInt(b *testing.T) {
var x []int64
var y []string
for i := 0; i < N; i++ {
x = append(x, int64(i))
y = append(y, strconv.Itoa(i))
}
}
// 22:29:22 ~/workspace/gocode/src/playground/typesizes 86$ go test -test.run=And -memprofile=mem.out -memprofilerate=1024 -test.v && go tool pprof --alloc_space typesizes.test mem.out
// === RUN TestStringAndInt
// --- PASS: TestStringAndInt (7.68s)
// PASS
// ok playground/typesizes 7.779s
// Entering interactive mode (type "help" for commands)
// (pprof) top
// 1269.12MB of 1269.16MB total ( 100%)
// Dropped 21 nodes (cum <= 6.35MB)
// flat flat% sum% cum cum%
// 1193.08MB 94.01% 94.01% 1269.12MB 100% playground/typesizes.TestStringAndInt
// 76.04MB 5.99% 100% 76.04MB 5.99% strconv.formatBits
// 0 0% 100% 1269.15MB 100% runtime.goexit
// 0 0% 100% 76.04MB 5.99% strconv.FormatInt
// 0 0% 100% 76.04MB 5.99% strconv.Itoa
// 0 0% 100% 1269.12MB 100% testing.tRunner
// (pprof) list And
// Total: 1.24GB
// ROUTINE ======================== playground/typesizes.TestStringAndInt in /Users/jpirruccello/workspace/gocode/src/playground/typesizes/main_test.go
// 1.17GB 1.24GB (flat, cum) 100% of Total
// . . 13:func TestStringAndInt(b *testing.T) {
// . . 14: var x []int64
// . . 15: var y []string
// . . 16:
// . . 17: for i := 0; i < N; i++ {
// 403.88MB 403.88MB 18: x = append(x, int64(i))
// 789.20MB 865.23MB 19: y = append(y, strconv.Itoa(i))
// . . 20: }
// . . 21:}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment