Last active
August 29, 2015 14:18
-
-
Save yosssi/622179a6c4de82f630a1 to your computer and use it in GitHub Desktop.
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 ( | |
"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 | |
} |
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 | |
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