title: Why is iterating over a map so much slower than iterating over a slice in Golang?
body: <p>I was implementing a sparse matrix using a map in Golang and I noticed that my code started taking much longer to complete after this change, after dismissing other possible causes, seems that the culprit is the iteration on the map itself. <a href="http://play.golang.org/p/rMOnwLjMwU" rel="noreferrer">Go Playground link</a> (doesn't work for some reason).</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "time"
      "math"
  )
  
  func main() {
      z := 50000000
      a := make(map[int]int, z)
      b := make([]int, z)
  
      for i := 0; i &lt; z; i++ {
          a[i] = i
          b[i] = i
      }
  
      t0 := time.Now()
      for key, value := range a {
          if key != value { // never happens
              fmt.Println("a", key, value)
          }
      }
      d0 := time.Now().Sub(t0)
  
      t1 := time.Now()
      for key, value := range b {
          if key != value { // never happens
              fmt.Println("b", key, value)
          }
      }
      d1 := time.Now().Sub(t1)
  
      fmt.Println(
          "a:", d0,
          "b:", d1,
          "diff:", math.Max(float64(d0), float64(d1)) / math.Min(float64(d0), float64(d1)),
      )
  }
  </code></pre>
  
  <p>Iterating over 50M items returns the following timings:</p>
  
  <pre><code>alix@local:~/Go/src$ go version
  go version go1.3.3 linux/amd64
  alix@local:~/Go/src$ go run b.go 
  a: 1.195424429s b: 68.588488ms diff: 17.777154632611037
  </code></pre>
  
  <p>I wonder, why is iterating over a map almost 20x as slow when compared to a slice?</p>