geektutu / interview-questions Goto Github PK
View Code? Open in Web Editor NEW机器学习/深度学习/Python/Go语言面试题笔试题(Machine learning Deep Learning Python and Golang Interview Questions)
Home Page: https://geektutu.com/post/qa-ml.html
机器学习/深度学习/Python/Go语言面试题笔试题(Machine learning Deep Learning Python and Golang Interview Questions)
Home Page: https://geektutu.com/post/qa-ml.html
关于该部分内容
interview-questions/qa-golang/qa-golang-1.md
Lines 298 to 331 in d4683a7
这里我试了一下,貌似 reflect.DeepEqual
的效率实际上是比 for 循环要高的
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
时,
$ 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 循环具有优势,但是优势也并不大
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.