Skip to content

Instantly share code, notes, and snippets.

@alberts
Created February 14, 2013 18:14
Show Gist options
  • Save alberts/4954884 to your computer and use it in GitHub Desktop.
Save alberts/4954884 to your computer and use it in GitHub Desktop.
package sse42
//#include <string.h>
//#cgo linux CFLAGS: -O3 -msse4.2
import "C"
import "unsafe"
func Equal(b1, b2 []byte) bool {
if len(b1) != len(b2) {
return false
}
if len(b1) == 0 {
return true
}
return C.strncmp((*C.char)(unsafe.Pointer(&b1[0])), (*C.char)(unsafe.Pointer(&b2[0])), C.size_t(len(b1))) == 0
}
package sse42
import (
"bytes"
"testing"
)
const size = 1 << 20
func BenchmarkEqual(b *testing.B) {
b1 := make([]byte, size)
b2 := make([]byte, size)
// this should make it panic?!
b1[1] = 1
b.ResetTimer()
for i := 0; i < b.N; i++ {
if !Equal(b1, b2) {
panic("failed")
}
}
}
func BenchmarkBytesEqual(b *testing.B) {
b1 := make([]byte, size)
b2 := make([]byte, size)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if !bytes.Equal(b1, b2) {
panic("failed")
}
}
}
func BenchmarkStringEqual(b *testing.B) {
s1 := string(make([]byte, size))
s2 := string(make([]byte, size))
b.ResetTimer()
for i := 0; i < b.N; i++ {
if s1 != s2 {
panic("failed")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment