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
- A Time Complexity Question
- Analysis of Algorithms | Set 4 (Analysis of Loops)
- Algorithm Practice Question for Beginners | Set 1
- An interesting time complexity question
- Count of integers that divide all the elements of the given array
- Maximum String Partition
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Minimum Possible sum of digits in a positive multiple of N
- Difference between Posteriori and Priori analysis
- How to learn Pattern printing easily?
- Find the smallest positive number missing from an unsorted array | Set 3
- Sum of all second largest divisors after splitting a number into one or more parts
- Count pairs of strings that satisfy the given conditions
- Minimum deletions required such that any number X will occur exactly X times