which one should be preferable from the following two nested for loops:

1.
for(i=0;i<100;++i)
{
for(k=0;k<10000;++k)
{
<lines of code>
}
}

2.
for(k=0;k<10000;++k)
{
for(i=0;i<100;++i)
{
<lines of code>
}
}

why?

Dani AI

Generated

Short answer: neither loop is universally "better" — choose the nesting that makes the inner loop access memory with unit stride and that matches the algorithmic dependency. For typical C/C++ row‑major storage, that means the inner loop should vary the column index so consecutive accesses touch consecutive memory. Algorithmic needs (what the inner code does) can force the opposite ordering; correctness or early exits trump micro-optimizations.

An example using a single contiguous buffer (common for performance) shows the preferred pattern for row‑major layout:

int rows = R, cols = C;
std::vector<int> data(rows * cols);

// good: inner loop moves across columns (unit stride)
for (int r = 0; r < rows; ++r) {
    int base = r * cols;
    for (int c = 0; c < cols; ++c) {
        process(data[base + c]);
    }
}

Contrast that with a column-major traversal, which induces a non-unit stride and more cache misses on row-major platforms:

for (int c = 0; c < cols; ++c) {
    for (int r = 0; r < rows; ++r) {
        process(data[r * cols + c]); // larger stride between elements
    }
}

Practical notes and when ordering doesn’t matter: if the inner work is heavy (complex math, blocking I/O, expensive function calls) the memory-order difference often becomes negligible. If the loops implement a logical row→column relationship (see ’s multiplication‑table example), preserve that for clarity. As and hinted, data layout and cache behaviour drive performance; for column‑major languages (Fortran, MATLAB) invert the rule. When parallelizing, watch false sharing and choose chunking that keeps threads working on separate cache lines.

If performance matters, measure: compile with optimization, run multiple timed trials, and, when possible, use a profiler (cache counters) to verify cache misses rather than relying on intuition.

Recommended Answers

All 5 Replies

Since both are the same, it will probably depend on what "<lines of code>" contains.

can you give an example as to how it will depend on the lines of codes

Example: Lets say you want to print out a multiplication table that has 10 rows and 5 columns. Rows are numbered 1 through 10 and columns numbered 1 through 5. The value in each column is the row number times the column number.

To print such a table you have to loop through each row, then inside that loop you have to loop through each column. It would be next to impossible to print the table if the two loops were reversed.

Also, if accessing data in a large 2D array, you should generally order the looping so that you work across rows, not down columns.

For a very technical answer:

which one should be preferable from the following two nested for loops:

The one that results in the most predictable and contiguous memory accessing pattern, to minimize cache misses and maximize pre-fetching.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.