Skip to content

Instantly share code, notes, and snippets.

@alberts
Created February 14, 2013 18:14

Revisions

  1. alberts created this gist Feb 14, 2013.
    16 changes: 16 additions & 0 deletions sse42.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    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
    }
    43 changes: 43 additions & 0 deletions sse42_test.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    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")
    }
    }
    }