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 1 You must be signed in to fork a gist
  • Save yosssi/622179a6c4de82f630a1 to your computer and use it in GitHub Desktop.
Save yosssi/622179a6c4de82f630a1 to your computer and use it in GitHub Desktop.
package main
import (
"reflect"
"strconv"
"testing"
)
const numOfValues = 100
var (
s1 = make([]string, numOfValues)
s2 = make([]string, numOfValues)
)
func init() {
for i := 0; i < numOfValues; i++ {
s := strconv.Itoa(i)
s1[i] = s
s2[i] = s
}
}
func TestDeepEqual(t *testing.T) {
if !reflect.DeepEqual(s1, s2) {
t.Error("DeepEqual(s1, s2) should be true")
}
}
func TestLoopEqual(t *testing.T) {
if !LoopEqual(s1, s2) {
t.Error("LoopEqual(s1, s2) should be true")
}
}
func BenchmarkDeepEqual(b *testing.B) {
for i := 0; i < b.N; i++ {
reflect.DeepEqual(s1, s2)
}
}
func BenchmarkLoopEqual(b *testing.B) {
for i := 0; i < b.N; i++ {
LoopEqual(s1, s2)
}
}
func LoopEqual(a, b []string) 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
}
$ go test -bench . -benchmem
PASS
BenchmarkDeepEqual 100000 19983 ns/op 3312 B/op 203 allocs/op
BenchmarkLoopEqual 3000000 433 ns/op 0 B/op 0 allocs/op
ok github.com/yosssi/comp_slice 3.940s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment