使用range来循环切片时,每一轮都会向同一个临时变量进行赋值即赋值拷贝,这是一个性能上的开销。从下面的测试输出能看得出临时变量的地址是不会变的,而且与切片的地址完全不同。如果切片元素包含字符串的话,用range遍历和直接寻址遍历的性能差别还是很显著的。
package main
import (
"fmt"
"time"
)
type Person struct {
Name string
Gender string
Experience string
}
func main(){
persons := make([]Person,8)
persons[0] = Person{"David","Male","This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!"}
persons[1] = Person{"Jenny","Female","This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!"}
persons[2] = Person{"Tom","Male","This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!"}
persons[3] = Person{"Lucy","Female","This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!"}
persons[4] = Person{"David","Male","This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!"}
persons[5] = Person{"Jenny","Female","This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!"}
persons[6] = Person{"Tom","Male","This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!"}
persons[7] = Person{"Lucy","Female","This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!"}
start := time.Now()
for _,person := range persons{
if person.Gender == "Male"{
fmt.Printf("%p %v\n", &person, person)
}
}
fmt.Printf("%v\n", time.Now().Sub(start))
start = time.Now()
for i:=0;i<len(persons);i++{
if persons[i].Gender == "Male"{
fmt.Printf("%p %v\n", &persons[i], persons[i])
}
}
fmt.Printf("%v\n", time.Now().Sub(start))
}
输出:
0xc00009a000 {David Male This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!}
0xc00009a000 {Tom Male This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!}
0xc00009a000 {David Male This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!}
0xc00009a000 {Tom Male This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!}
112.416µs
0xc000094180 {David Male This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!}
0xc0000941e0 {Tom Male This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!}
0xc000094240 {David Male This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!}
0xc0000942a0 {Tom Male This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!This is a long text!}
22.313µs
除了效率问题以外,for range这种把值拷贝给临时变量的机制,在操作切片元素地址的时候要特别小心,下面是一道面试题:
2 以下代码有什么问题,说明原因。
type student struct {
Name string
Age int
}
func pase_student() {
m := make(map[string]*student)
stus := []student{
{Name: "zhou", Age: 24},
{Name: "li", Age: 23},
{Name: "wang", Age: 22},
}
for _, stu := range stus {
m[stu.Name] = &stu
}
fmt.Printf("%v\n",m)
}
for range这种循环写法在会创建一个临时变量,每轮循环都把元素值拷贝给该临时变量,所以&stu始终指向这个临时变量的地址,循环结束后m里的value将全部等于这个临时变量的地址,对应的取值等于切片的最后一个元素的取值。这种需要取地址的循环只能用直接寻址的写法:for i:=0;i<len(stus);i++{m[stus[i].name]=&stus[i]}