   "Slow" 2D Arrays
  So say I have 10 3D points that I want to store in a 2D array.I could do so either of these ways:float p[10][3];float p[3][10];I remember from one of my classes that one is a lot slower than the other because of spatial locality (if I'm using one dimension of a point I'll be using the other dimensions as well). I think the first is the slower, but I almost remember it being counter-intuitive. Please confirm?Am I right in thinking that the faster one (whichever that is) is essentially identical to float p[3*10]?   
  Hi Schnozzinkobenstein:In the first case, you have 10 blocks of 3 floats. 10 small allocations in different places. This can cause more memory fragmentation, and is less cache friendly IMO.In the second case you are allocating 3 blocks of 10 floats. Less blocks and bigger allocations. Less memory fragmentation and more cache friendly.And finally, float p[3*10] is faster than other two because it allocates only one block of 30 floats.But all this don't matter in your case because you have to store points in a specific way. Think about this:float p[10][3]. In each block you store one point. Whenever you try to get any point, you are going to have faster access, just one block. Also, you can pass pointers to points( &p[3] for example gives you a pointer to the fourth point). In the other hand, with p[3][10], i assume you are going to store each component (x,y,z) in different blocks, so whenever you want to access one point, you need to access all three blocks.I would use last case (p[3*10]). I think that it's the way that vertexArray uses.   ;  Both are identical to int[x*y]. What matters is how you use the space, not how you declare it.The second is slower; int[x][y] stores X runs of Y entries, so [10][3] will store XYZXYZXYZXYZXYZXYZXYZXYZXYZXYZ while [3][10] will store XXXXXXXXXXYYYYYYYYYYZZZZZZZZZZ. Because you'll be using all three XYZ values at a time, keeping them together means they're more likely to all be in the same cache line.Bear in mind that by 'faster' and 'slower' in these circumstances, though, we're talking about some really miniscule speed differences. It's usually much more important to focus on keeping your code readable and well-structured; if it's readable and well-structured, then coming back to improve performance later will be much easier. It's likely that in the example you've given, it'll make no difference whatsoever, because the whole array will easily fit into a single cache line either way; in fact you may even find you want to store it the 'wrong' way because that makes it easier to write SSE-optimized code.These are decisions to be made later, once your application is working. First, make it work. Then make it fast.;Quote:Original post by JuanxoIn the first case, you have 10 blocks of 3 floats. 10 small allocations in different places. This can cause more memory fragmentation, and is less cache friendly IMO.No, it's not 10 allocations in different places: it's a single contiguous block of memory. Multidimensional arrays - when declared with fixed sizes like this - aren't broken down by the compiler into separate allocations; it always calculates (x*y), and reserves or allocates a block for the entire thing in one go.   ;  What's wrong withstruct Point{  float x, y, z;};Point p[10];??or did I miss something?   ;  Nothing - that's probably better than using a 2D array. But, the OP says he wants to store things in a 2D array, so I'm assuming he's got some reason for that.   ;  Thanks for the help.Why would that struct be better other than for readability?   ;Quote:Original post by SchnozzinkobensteinThanks for the help.Why would that struct be better other than for readability?E.g., because then you can express your intent, whereas arrays are just continuous swadrons of values.   ;Quote:Original post by SchnozzinkobensteinWhy would that struct be better other than for readability?You can sort an array of struct, but you cannot sort an array of arrays (because arrays are not assignable).   ;  Would the xyz elements in that struct be likely to have as much spacial locality as a 1d array would?Quote:You can sort an array of struct, but you cannot sort an array of arrays (because arrays are not assignable).I'm not sure what you mean by that.   