Minor C ++ optimization behaves strangely

I am trying to optimize "in the small" on my project.

There is a series of array accesses that are individually tiny, but profiling has shown that these array accesses are where the vast majority of my program spends its time. So, the time to speed up the work, as it takes about an hour to complete the program.

I moved the following type of access:

const float theValOld1 = image(x, y, z);
const float theValOld2 = image(x, y+1, z);
const float theValOld3 = image(x, y-1, z);
const float theValOld4 = image(x-1, y, z);

etc., for 28 hits around the current pixel.

where the image sticks up

float image(const int x, const int y, const int z) const {
     return data[z*xsize*ysize + y*xsize + x];
}

and I replaced it

const int yindex = y*xsize;
const int zindex = z*xsize*ysize;
const float* thePtr = &(data[z*xsize*ysize + y*xsize + x]);
const float theVal1 = *(thePtr);
const float theVal2 = *(thePtr + yindex);
const float theVal3 = *(thePtr - yindex);
const float theVal4 = *(thePtr - 1);

etc. for the same number of operations.

, , , . , , , , implict, [] thunk, y z .

, z , , zindex!= 0, , 9 . , , , .

, QueryPerformanceCounter.

, !

( , , ), 45 , 10 . , 14 , 30 ( , 100 ).

? - ? , , . - , - ?

. , , - , . , , - , . , , , ()?

EDIT: .

, , , .

, , . , , , .

z , if . z- z, , , - 100% 2D. 3D-, , .

+3
4

, , , - . , , , L1, , L2, L3, . , , , .

- , , , , , . ( , . , , ). , , , , , . , 3 , , . .

? - ? , , . - , - ?

. , . , - , . , .

- , X T, , X + 1 ( ), , , X , T + 1 ( ). ( ), .

, z , , zindex!= 0, , 9 . , , , .

, if, , , . , . , . , , .

, , , ? , . image(x, y, z), image(x+1, y, z), , . 9 X/Y ( 3 , 3 , , 3 )

, . , ( ), , , , , , , .

, , , CPU ( , , , CPU , ).

. ( z*xsize*ysize, xsize*ysize z .

, , - . - , , , . , , , ( , )

, , . , /, , , , .

, . operator[] , , , , .

, , , . , - , , / . , , .

, , . , .

( , - , ?) , ?)

, (, , )

+5

() - . , , "" ( ), CPU (32, 64 128 , ). .

, - , .

- - "" .

, , , ( ). , - - - ...

, , (, AMD CodeAnalyst).

, - , , , -, , , , , , .. ( , "", .)

+8

.

:

240

<x,y,z> 10,10,0

:

a. data[0+ 10*240 + 10] -> data[2410]
b. data[0+ 11*240 + 10] -> data[2650]
c. data[0+  9*240 + 10] -> data[2170]
d. data[0+ 10*240 +  9] -> data[2409]

, .

. , (, c,d,a,b), .

, . 100 , . .

+4

, z , , zindex!= 0, , 9 . , , , .

? , . if - . "IF" ?. , , , z 99% .

, !

? , . , .

+3

Source: https://habr.com/ru/post/1714170/


All Articles