Skip to content

Instantly share code, notes, and snippets.

@artyom
Created July 10, 2014 14:39
Show Gist options
  • Save artyom/ccfd0a8578d3196c0d9f to your computer and use it in GitHub Desktop.
Save artyom/ccfd0a8578d3196c0d9f to your computer and use it in GitHub Desktop.
Golang XOR performance for 8/64 bytes long integers
$ go test -bench .
PASS
Benchmark8 1000000 1093 ns/op 936.69 MB/s
Benchmark64 20000000 134 ns/op 7603.74 MB/s
ok _/private/tmp/x 3.944s
package x
import (
"bytes"
"encoding/binary"
"testing"
)
func TestLen(t *testing.T) {
if len(A) != len(B) {
t.Fatal("length of a and b differs")
}
}
func Benchmark8(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := range A {
_ = A[j] ^ B[j]
}
}
b.SetBytes(int64(len(A)))
}
func Benchmark64(b *testing.B) {
var au, bu []uint64
au = make([]uint64, len(A)/8)
bu = make([]uint64, len(B)/8)
if err := binary.Read(bytes.NewReader(A), binary.LittleEndian, au); err != nil {
b.Fatal(err)
}
if err := binary.Read(bytes.NewReader(B), binary.LittleEndian, bu); err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
for j := range au {
_ = au[j] ^ bu[j]
}
}
b.SetBytes(int64(len(A)))
}
var A, B []byte
func init() {
A = make([]byte, 1024)
B = make([]byte, 1024)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment