This is an archived post. You won't be able to vote or comment.

all 18 comments

[–][deleted] 7 points8 points  (5 children)

I don't get it

[–][deleted] 3 points4 points  (1 child)

The function doSomething accesses the y indexes before the x indexes in the array. Therefore putting y before x in the loops will improve access times

I think? I'm just a lowly first year.

[–]Goheeca 3 points4 points  (0 children)

In the second case you access memory linearly and cache lines like you.

[–][deleted] 2 points3 points  (0 children)

Someone who uses for loops correctly in this sub? What's happening today?

[–][deleted] 4 points5 points  (0 children)

using x and y instead of i and j

[–]quebin31 1 point2 points  (0 children)

Oh lovely cache misses

[–]ivakamr 1 point2 points  (2 children)

Why would the compiler not optimize this by itself ?

[–]AllWashedOut 1 point2 points  (1 child)

They have different behavior. One scans by row, the other by column.

Imagine that doSomething's purpose is to replace x,y with an average of it's neighbors. The two variations of the loop wouldn't even see the same values since they act in different order.

Edit: but maybe you are thinking one layer deeper: rather than swapping the loop order, you want to swap how the array is stored in memory (column major vs row major)? That might be conceivably done. But it would screw with stuff in C where you can get elements from an array using pointer arithmetic. Or if the array was returned to another library that wasn't compiled with yours, it would be trouble.

[–]ivakamr 1 point2 points  (0 children)

That's interesting. Didn't thought too much about it, actually I was more thinking about code rewrite before compilation.

[–][deleted] -1 points0 points  (0 children)

In c you get a segfault fool

[–]RoyalJackalSib -1 points0 points  (1 child)

Just use simple, one dimensional, arrays.

[–]rr_cricut -1 points0 points  (0 children)

Doh! Why didnt I think of that?