Consider below two C language functions to compute sum of elements in a 2D array. Ignoring the compiler optimizations, which of the two is better implementation of sum?
In C/C++, elements are stored in Row-Major order. So the first implementation has better spatial locality (nearby memory locations are referenced in successive iterations). Therefore, first implementation should always be preferred for iterating multidimensional arrays.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Analysis of Algorithms | Set 4 (Analysis of Loops)
- Algorithm Practice Question for Beginners | Set 1
- An interesting time complexity question
- A Time Complexity Question
- Examples of Big-O analysis
- The Slowest Sorting Algorithms
- Subset Sum is NP Complete
- Time-Space Trade-Off in Algorithms
- Optimized Longest Path is NP Complete
- Proof that 4 SAT is NP complete
- Proof that Collinearity Problem is NP Complete
- Subset Equality is NP Complete
- Proof that SAT is NP Complete
- Double SAT is NP Complete