Last active
August 29, 2015 14:18
-
-
Save yosssi/bace0ca6c231cd3c2bfe to your computer and use it in GitHub Desktop.
Comparison of byte slices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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