Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"testing"
)
var data = [64]int{}
var data2 = [64]int{}
func BenchmarkOffset2(b *testing.B) {
pos := 32
s := data[:]
for i := 0; i < b.N; i++ {
k := pos+32
for i := 0; i < k; i++ {
s[i] = i
}
}
}
func BenchmarkOffset(b *testing.B) {
pos := 32
s := data[:]
for i := 0; i < b.N; i++ {
for i := 0; i < pos+32; i++ {
s[i] = i
}
}
}
func BenchmarkRange(b *testing.B) {
pos := 32
s := data[:]
for i := 0; i < b.N; i++ {
a := s[pos : pos+32]
for i, _ := range a {
a[i] = i
}
}
}
func BenchmarkUnrolled(b *testing.B) {
pos := 32
s := data[:]
for i := 0; i < b.N; i++ {
s[pos+0] = 0
s[pos+1] = 1
s[pos+2] = 2
s[pos+3] = 3
s[pos+4] = 4
s[pos+5] = 5
s[pos+6] = 6
s[pos+7] = 7
s[pos+8] = 8
s[pos+9] = 9
s[pos+10] = 10
s[pos+11] = 11
s[pos+12] = 12
s[pos+13] = 13
s[pos+14] = 14
s[pos+15] = 15
s[pos+16] = 16
s[pos+17] = 17
s[pos+18] = 18
s[pos+19] = 19
s[pos+20] = 20
s[pos+21] = 21
s[pos+22] = 22
s[pos+23] = 23
s[pos+24] = 24
s[pos+25] = 25
s[pos+26] = 26
s[pos+27] = 27
s[pos+28] = 28
s[pos+29] = 29
s[pos+30] = 30
s[pos+31] = 31
}
}
func BenchmarkCopy(b *testing.B) {
pos := 32
s := data[:]
for i := 0; i < b.N; i++ {
copy(data2[pos:pos+32], s[pos:pos+32])
}
}
func BenchmarkRangeCopy(b *testing.B) {
pos := 32
s := data[:]
for i := 0; i < b.N; i++ {
a := s[pos:pos+32]
for i, _ := range a {
a[i] = data2[i]
}
}
}
func BenchmarkCopyUnrolled(b *testing.B) {
pos := 32
s := data[:]
for i := 0; i < b.N; i++ {
s[pos+0] = data2[pos+0]
s[pos+1] = data2[pos+1]
s[pos+2] = data2[pos+2]
s[pos+3] = data2[pos+3]
s[pos+4] = data2[pos+4]
s[pos+5] = data2[pos+5]
s[pos+6] = data2[pos+6]
s[pos+7] = data2[pos+7]
s[pos+8] = data2[pos+8]
s[pos+9] = data2[pos+9]
s[pos+10] = data2[pos+10]
s[pos+11] = data2[pos+11]
s[pos+12] = data2[pos+12]
s[pos+13] = data2[pos+13]
s[pos+14] = data2[pos+14]
s[pos+15] = data2[pos+15]
s[pos+16] = data2[pos+16]
s[pos+17] = data2[pos+17]
s[pos+18] = data2[pos+18]
s[pos+19] = data2[pos+19]
s[pos+20] = data2[pos+20]
s[pos+21] = data2[pos+21]
s[pos+22] = data2[pos+22]
s[pos+23] = data2[pos+23]
s[pos+24] = data2[pos+24]
s[pos+25] = data2[pos+25]
s[pos+26] = data2[pos+26]
s[pos+27] = data2[pos+27]
s[pos+28] = data2[pos+28]
s[pos+29] = data2[pos+29]
s[pos+30] = data2[pos+30]
s[pos+31] = data2[pos+31]
}
}
--- Output of using **gccgo -O3** ---
BenchmarkOffset2 50000000 27.6 ns/op
BenchmarkOffset 50000000 32.0 ns/op
BenchmarkRange 500000000 4.46 ns/op
BenchmarkUnrolled 200000000 8.92 ns/op
BenchmarkCopy 500000000 7.52 ns/op
BenchmarkRangeCopy 100000000 13.9 ns/op
BenchmarkCopyUnrolled 200000000 8.91 ns/op
--- Output of using standard go compiler ---
BenchmarkOffset2 20000000 137 ns/op
BenchmarkOffset 20000000 137 ns/op
BenchmarkRange 50000000 31.3 ns/op
BenchmarkUnrolled 100000000 13.9 ns/op
BenchmarkCopy 100000000 10.5 ns/op
BenchmarkRangeCopy 50000000 44.6 ns/op
BenchmarkCopyUnrolled 50000000 30.4 ns/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment