You have a 2d array with visual coordinates. Which do you prefer?

You have a 2d array with visual coordinates. Which do you prefer?

myGrid[y][x]
myGrid[x][y]

Other urls found in this thread:

youtube.com/watch?v=NH1Tta7purM
twitter.com/AnonBabble

i usually do the first but the second looks nicer

[y][x].
In many cases, the access is faster.

(

>In many cases, the access is faster.
Source?

no its not you just have to change the order when you acces the elements and you get the same speed.

none is faster than the other you just have to acces the elements in the right order with both.

youtube.com/watch?v=NH1Tta7purM

Indeed.

Too lazy to look for it, but it's quite simple.
Every step on the second array stuff jumps (datasize) bytes, every step on the first array stuff jumps (datasize)*total size of the second array.
Caches and memory like their shit accessed linearly, because it's easier to predict than shit jumping everywhere on the memory.

Depends on the language.

On Java n dim arrays are array of array ... n.
Taken m[x][y]
For example if y is small but x is big, you will had huge x in not cache memory and a lot tiny arrays on y with overhead,bad cache and a lot work for GC.

If reverse x and y m[y][x], will had fast look up on cache small y and huge block arrays on x

Create it with one malloc() and it won't matter

myGrid[x][y]

and then when accessing
for (jj=0; jj

What's the reasoning behind ii and jj convension? Or is that just exemplary?

Why would you allocate that on the heap when you know how large the grid is?

>this is what C programmers really believe

>exemplary
you need to double check the meaning of that word

always x,y unless you are doing math that involves matrices

>you need to double check the meaning of that word

Well shit...

I used [x][y] for gamedev but then iterated y x. No reasoning, just made sense, and I wasn't writing anything that really needed to be optimised - it was mostly generating and using small grids.

myGrid[row][col]
i.e. myGrid[y][x]

row/column. Cartesian coordinates don't make sense if you're not using a proper cartesian system, a basic 2D plane isnt inherently cartesian and in fact most computing systems use a 0.0 to 1.0 system on both axis.

myGrid[x+y*width]

>what is cpu caching?

mygrid + y * width * sizeof(int) + x * sizeof(int)

What does normalizing the values have to do with it being cartesian or not? That's just converting the range right?

uint *array = (uint *)malloc(sizeof(uint)*width*height)
uint *pointer = array

*pointer++

megread[latitude, longitude]

CuckholdFactory.PointAccessor accessor = new EnterpriseCuckFactory.requestAccessorObjectFromShillOverloads();
if (accessor.shillingGranted())
try {
accessor.tryMove(x, y);
} catch (FactoryCuckEnterpriseException e) {}

Y then x makes most sense. Imagine youre building an image line by line, doing it vertically (row by row) makes more sense than doing it horizontally

This. Fortran and C/C++ have different access patterns. Usually the Compiler optimizes it by itself.

It depends on the math you are going to use. That completly changes the memorry arrangemebt and what will the on the CPU cache at the same time.
That decision actually has a huge impact on performance! Which means it depends on the algoritms you are going to run on the matrix!

>Usually the Compiler optimizes it by itself.
very unlikely unless your algorithm is trivial (std::fill or something)

you have a matrix and you use arrays?

Can someone explain to me why would it matter performance wise?

There is no difference between y,x and x,y and a,b - what does it matter what the variables are called? What they represent is different but the computer doesn't really care right? In the end it's just two numbers?

It's about how you iterate over that collection. Your X might have a huge range, but each Y only a small range. In a multi-dimension array, do you iterate Y first or X (nested of course)?

so it's not really a matter of y or x being first, rather a matter putting the smaller one first

It's about memory layout and caching. You'd prefer to access sequentially instead of pic related.

oh i get it, thanks

it's so simple in retrospect.

Depends on iteration. The inside array is kept togheter, so you inner iteration should be on this array so the CPU can do everything in cache witjou needing constant ram calls.
This makes some loops really faster!
A common exercise to prove this is matrix multiplication.