Skip to content

Instantly share code, notes, and snippets.

@alphazero
Created May 15, 2015 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphazero/28508de78a3fa549768d to your computer and use it in GitHub Desktop.
Save alphazero/28508de78a3fa549768d to your computer and use it in GitHub Desktop.
package foo
import (
"fmt"
"testing"
)
const M = 32768
const M0 = M / 8192 // play with this knob ...
var arr, arr0 [M]int
func init() {
fmt.Printf("----- using subset %d of %d\n", M0, M)
}
/// test access pattrens //////////////////////////////
func BenchmarkSlice(b *testing.B) {
for n := 0; n < b.N; n++ {
passSliceUseSlice(arr[:], arr0[:])
}
}
func BenchmarkSliceSpecBounds(b *testing.B) {
for n := 0; n < b.N; n++ {
passSliceUseSlice(arr[0:M], arr0[0:M])
}
}
func BenchmarkArrayPtr(b *testing.B) {
for n := 0; n < b.N; n++ {
passArraryPtrUseDeref(&arr, &arr0)
}
}
func BenchmarkArrayPtrToSlice(b *testing.B) {
for n := 0; n < b.N; n++ {
passArraryPtrUseSlice(&arr, &arr0)
}
}
/// access pattrens ///////////////////////////////////
// note: Mo-2 to allow for M0 == M
func passSliceUseSlice(s, s0 []int) {
for i := 0; i < M0-2; i++ {
arr[i] = arr[2+i]
}
}
func passArraryPtrUseSlice(arr, arr0 *[M]int) {
var s = (*arr)[:] // don't spec bounds here ..
var s0 = (*arr)[:]
for i := 0; i < M0-2; i++ {
s[i] = s0[2+i]
}
}
func passArraryPtrUseDeref(arr, arr0 *[M]int) {
for i := 0; i < M0-2; i++ {
(*arr)[i] = (*arr0)[2+i]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment