Go学习:切片slice的循环机制及性能

2 篇文章 0 订阅

使用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]}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值