Which of these two dimensional array are advantageous to use and why?

后端 未结 5 1928
故里飘歌
故里飘歌 2021-01-26 18:54

In a facebook group, I saw a question like :

If a row dominated two dimensional array in the following which one is advantage and why?

         


        
5条回答
  •  逝去的感伤
    2021-01-26 19:39

    Variant (a) is better, since it's much more cache-friendly, than (b): you access consecutive elements, so there are less cache misses. However, some compilers can rearrange your loops, so when compiled both variants might produce same machine codes.

    Besides performance, there is no difference between this two variants, i.e. thay produce the same output.

提交回复
热议问题