Skip to content

Instantly share code, notes, and snippets.

@davecheney
Created March 17, 2011 08:05
Show Gist options
  • Save davecheney/873990 to your computer and use it in GitHub Desktop.
Save davecheney/873990 to your computer and use it in GitHub Desktop.
package main
import "testing"
var (
array [1000000]int
slice = make([]int, 1000000)
Map = make(map[int]int)
r int // to avoid the compiler optimising away the access
)
func init() {
for i, _ := range array {
array[i] = i
slice[i] = i
Map[i] = i
}
}
func TestDummy(t *testing.T) {
println(r) // force
}
func BenchmarkArrayAccess(b *testing.B) {
for i := 0; i < 1000000; i++ {
r = array[i]
}
}
func BenchmarkSliceAccess(b *testing.B) {
for i := 0; i < 1000000; i++ {
r = slice[i]
}
}
func BenchmarkMapAccess(b *testing.B) {
for i := 0; i < 1000000; i++ {
r = Map[i]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment