When processing images, why is it recommended to first iterate over Y, and then X seconds?

I read in several places that when processing pixels in an image, it is recommended to iterate over Y, and then X pixels, because it is more memory efficient. Why is this so?

+4
source share
2 answers
A1 A2 A3 A4 
B1 B2 B3 B4 
C1 C2 C3 C4 
D1 D2 D3 D4 

Assuming that your image (and some coordinates to it indicates), it is stored in memory in a line, as:

A1 A2 A3 A4 B1 B2 B3 B4 C1 C2 C3 C4 D1 D2 D3 D4.

Let me simulate these parameters:

For X, and then through Y:

A1 B1 C1 D1 A2 B2 C2 D2 A3 B3 C3 D3 A4 B4 C4 D4 

Now check the line how messy the access will be (like random reading, right?)

Not, for Y, and then through X

A1 A2 A3 A4 B1 B2 B3 B4 C1 C2 C3 C4 D1 D2 D3 D4

Cm? Read it straight ahead!

That's why.

+4
source

, .
, . , , .


, , , CPU . 8 16 cache line.
, , " ", , .

, CPU , .


, , , . , "" .

cache lines wikipedia.

-
, "" , .
- , , .

+4

All Articles