直接上代码
package main import ( l "container/list" "fmt" "time" ) var ( name = "viney" ) func list() { names := l.New() t := time.Now() for i := 1; i <= 1000000; i++ { _ = names.PushFront(name) } fmt.Println("list: " + time.Now().Sub(t).String()) } func slice() { names := []string{} t := time.Now() for i := 1; i <= 1000000; i++ { names = append(names, name) } fmt.Println("slice: " + time.Now().Sub(t).String()) } func main() { list() slice() }测试的不是很权威,不过初步淡定,slice的效率比list快两倍。
有疑问加站长微信联系(非本文作者)
![](https://static.golangjob.cn/static/img/footer.png?imageView2/2/w/280)