Skip to content

Instantly share code, notes, and snippets.

@davecheney
Created November 7, 2013 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save davecheney/7346656 to your computer and use it in GitHub Desktop.
Save davecheney/7346656 to your computer and use it in GitHub Desktop.
package arrayslice
import "testing"
func BenchmarkArray8for(b *testing.B) {
var a [8]int
for i := 0 ; i < b.N ; i++ {
for j := 0 ; j < len(a) ; j++ {
a[j] += j
}
}
}
func BenchmarkArray8range(b *testing.B) {
var a [8]int
for i := 0 ; i < b.N ; i++ {
for i := range a {
a[i] += i
}
}
}
func BenchmarkSlice8for(b *testing.B) {
var s = []int{0,0,0,0,0,0,0,0}
for i := 0; i < b.N ; i++ {
for j, n := 0, len(s) ; j < n; j++ {
s[j] += j
}
}
}
func BenchmarkSlice8range(b *testing.B) {
var s = []int{0,0,0,0,0,0,0,0}
for i := 0; i < b.N ; i++ {
for i := range s {
s[i] += i
}
}
}
func BenchmarkStruct8(b *testing.B) {
var t = struct { i0, i1, i2, i3, i4, i5, i6, i7 int }{}
for i := 0; i < b.N ; i++ {
t.i0 += 0
t.i1 += 1
t.i2 += 2
t.i3 += 3
t.i4 += 4
t.i5 += 5
t.i6 += 6
t.i7 += 7
}
}
lucky(~/src/github.com/davecheney/arrayslice) % go test -bench=.
testing: warning: no tests to run
PASS
BenchmarkArray8for 500000000 7.92 ns/op
BenchmarkArray8range 100000000 10.7 ns/op
BenchmarkSlice8for 200000000 8.76 ns/op
BenchmarkSlice8range 200000000 8.66 ns/op
BenchmarkStruct8 1000000000 2.70 ns/op
ok github.com/davecheney/arrayslice 14.076s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment