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++)
{
for (int j = 0; j < 5; j++)
{
count++;
}
}
Case 2: assume int count = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 10; j++)
{
count++;
}
}
In both the scenario’s the final valuse of count will be 50.
But I am not sure which one will be faster? I think CASE II is faster but not sure…
It will be great if anyone can throw some light on it. which is faster and why?
this is the only example i can think of where it matters which variable you iterate where
the second one is slower because you don’t iterate the locations in memory one after the other, but because you jump by
mlocations at a time. the processor caches the memory locations positioned immediately after an accessed location.