Skip to content

Instantly share code, notes, and snippets.

@yosssi
Last active August 29, 2015 14:18
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 yosssi/bace0ca6c231cd3c2bfe to your computer and use it in GitHub Desktop.
Save yosssi/bace0ca6c231cd3c2bfe to your computer and use it in GitHub Desktop.
Comparison of byte slices
package main
import (
"bytes"
"testing"
)
var d = make([]byte, 256)
func init() {
for i, l := 0, len(d); i < l; i++ {
d[i] = 0
}
}
func loopEqual(a, b []byte) bool {
al, bl := len(a), len(b)
if al != bl {
return false
}
for i := 0; i < al; i++ {
if a[i] != b[i] {
return false
}
}
return true
}
func Benchmark_loop(b *testing.B) {
for i := 0; i < b.N; i++ {
loopEqual(d, d)
}
}
func Benchmark_bytesEqual(b *testing.B) {
for i := 0; i < b.N; i++ {
bytes.Equal(d, d)
}
}
$ go test -bench . -benchmem
testing: warning: no tests to run
PASS
Benchmark_loop 10000000 203 ns/op 0 B/op 0 allocs/op
Benchmark_bytesEqual 100000000 11.0 ns/op 0 B/op 0 allocs/op
ok github.com/yosssi/comp_byte 3.358s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment