Order of the code and performance

后端 未结 2 979
独厮守ぢ
独厮守ぢ 2020-11-30 14:57

I wanted to find which is faster: struct vs array. So I wrote a GO code in which I write 4 int values (1,2,3 and 4) to the members of a structure and then to an array of len

2条回答
  •  自闭症患者
    2020-11-30 15:18

    The other answer explained the timing difference, let's get into struct vs. slice.

    If the compiler can figure out at compile time that the slice is big enough, accessing the elements of the slice and a struct will generate identical code. Of course, in reality, often the compiler won't know how big the slice is and completely different optimizations will be applied depending on if you're working with a struct or a slice, so for measuring performance you have to look at a whole program and its behavior, not just one particular operation.

提交回复
热议问题