I do some calculations in a 2D array and have to go through the array in four different ways.
for(int i=0; i < array_size; i++) { for(int j=0; j < array_size; j++) { #do some computation around [i][j] element } } for(int i = array_size - 1; i >= 0; i--) { for(int j=0; j < array_size; j++) { #do the same computation around [i][j] element } } for(int i=0; i < array_size; i++) { for(int j=array_size - 1; j >= 0; j--) { #do the same computation around [i][j] element } } for(int i = array_size - 1; i >=0; i--) { for(int j = array_size - 1; j >= 0; j--) { #do the same computation around [i][j] element } }
The fact is that, firstly, the calculation code is long and may also be changed in the future. Secondly, arrays are huge, so performance is also a problem.
I was wondering if there is a way to avoid code duplication and maintain performance. Since fetching code into a function is likely to slow performance.
source share