Skip to content

Instantly share code, notes, and snippets.

@komuw
Last active January 25, 2018 14:26
Show Gist options
  • Select an option

  • Save komuw/4f1ca71a54e49d7187d0fa30ebdc9a2b to your computer and use it in GitHub Desktop.

Select an option

Save komuw/4f1ca71a54e49d7187d0fa30ebdc9a2b to your computer and use it in GitHub Desktop.
measure GC for different maps
package main
import (
"strconv"
"fmt"
"os"
"runtime"
"time"
)
// run this program as:
/*
#!/bin/bash
for t in 1 2; do go run gctest.go $t; done
*/
func timeGC() time.Duration {
start := time.Now()
runtime.GC()
return time.Since(start)
}
func main() {
const N = 30e6
if len(os.Args) != 2 {
fmt.Printf("usage: %s [1 2]\n(number selects the test)\n", os.Args[0])
return
}
switch os.Args[1] {
case "1":
// create a big map of strings. since strings contain pointers, we expect this to have more pressure on GC.
m := make(map[string]string)
for i := 0; i < N; i++ {
n := strconv.Itoa(i)
m[n] = n
}
runtime.GC()
fmt.Printf("With a map of strings, GC took: %s\n", timeGC())
_ = m["0"]
case "2":
// create a map of ints. We want to store strings in the map but unlike in case 1, we will hash the strings to ints (which have no pointer)
// so we expect less pressure on GC. We are using strconv.Atoi(str) as a stand-in for a proper hash function
m := make(map[int]int)
for i := 0; i < N; i++ {
str := strconv.Itoa(i)
// hash string to int
n,_ := strconv.Atoi(str)
m[n] = n
}
runtime.GC()
fmt.Printf("With map of strings(that are hashed to ints), GC took:: %s\n", timeGC())
_ = m[0]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment