How to find out which nested for loop is better?

前端 未结 5 1076
无人及你
无人及你 2021-01-21 16:14

Somebody asked me a question: Which one is the fastest among the below two scenario:

Case 1: assume int count = 0;

for (int i = 0; i < 10; i++)
{
           


        
5条回答
  •  猫巷女王i
    2021-01-21 16:54

    this is the only example i can think of where it matters which variable you iterate where

    int array[n][m];
    //fast
    for (int i=0;i

    the second one is slower because you don't iterate the locations in memory one after the other, but because you jump by m locations at a time. the processor caches the memory locations positioned immediately after an accessed location.

提交回复
热议问题