We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@geektutu
关于该部分内容
interview-questions/qa-golang/qa-golang-1.md
Lines 298 to 331 in d4683a7
这里我试了一下,貌似 reflect.DeepEqual 的效率实际上是比 for 循环要高的
reflect.DeepEqual
package main import ( "math/rand" "reflect" "testing" "time" ) var ( l = 100000 l2 = 100 s1 = generate(l) s2 = generate(l) s3 = generate(l * 2) ) var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") func randStringRunes(n int) string { rand.Seed(time.Now().UnixNano()) b := make([]rune, n) for i := range b { b[i] = letterRunes[rand.Intn(len(letterRunes))] } return string(b) } func generate(n int) []string { s := make([]string, n) for i := 0; i < n; i++ { s[i] = randStringRunes(l2) } return s } func cmp1(a, b []string) bool { return reflect.DeepEqual(a, b) } func cmp2(a, b []string) bool { if len(a) != len(b) { return false } if (a == nil) != (b == nil) { return false } b = b[:len(a)] for i, v := range a { if v != b[i] { return false } } return true } func BenchmarkReflect(b *testing.B) { for i := 0; i < b.N; i++ { cmp1(s1, s2) cmp1(s1, s3) cmp1(s1, s1) } } func BenchmarkFor(b *testing.B) { for i := 0; i < b.N; i++ { cmp2(s1, s2) cmp2(s1, s3) cmp2(s1, s1) } }
在 l = 100000,l2=100时,
l = 100000
l2=100
$ go test -bench=. goos: linux goarch: amd64 BenchmarkReflect-4 1235326 856 ns/op BenchmarkFor-4 2890 354342 ns/op PASS
在 l = 100,l2=100000时,
l = 100
l2=100000
$ go test -bench=. goos: linux goarch: amd64 BenchmarkReflect-4 1205154 866 ns/op BenchmarkFor-4 3315399 373 ns/op PASS
好像只有切片长度很短时,for 循环具有优势,但是优势也并不大
The text was updated successfully, but these errors were encountered:
No branches or pull requests
@geektutu
关于该部分内容
interview-questions/qa-golang/qa-golang-1.md
Lines 298 to 331 in d4683a7
这里我试了一下,貌似
reflect.DeepEqual
的效率实际上是比 for 循环要高的在
l = 100000
,l2=100
时,$ go test -bench=. goos: linux goarch: amd64 BenchmarkReflect-4 1235326 856 ns/op BenchmarkFor-4 2890 354342 ns/op PASS
在
l = 100
,l2=100000
时,$ go test -bench=. goos: linux goarch: amd64 BenchmarkReflect-4 1205154 866 ns/op BenchmarkFor-4 3315399 373 ns/op PASS
好像只有切片长度很短时,for 循环具有优势,但是优势也并不大
The text was updated successfully, but these errors were encountered: