Awesome. So a flattened 3D to 1D matrix has negligible performance differences compared to similar matrices and access methods?
|
Pretty much. you can always noob it up, but at the end of the day, you have a starting point and an offset from there. As long as its one block of memory, that is about as efficient as it gets one way or another. If you access it wrong (eg, going down one column in a 2-d) you can have performance problems (the computer and design are made to go across a row, not down a column, and youll pay for it to go the other way, consider storing it transposed if you always do it the wrong way).
I think you are referring to a comment I made, and you didn't understand it. You can exceed a PAGE of memory, one 'block' a the operating system / hardware level. Pages are actually quite small and you WILL do this frequently. Not a problem, as long as you use one page to death before getting another one (see above on running down columns, that would cause it to grab pages more often inefficiently). When I say a block of memory, I mean that your array starts at some location, and the last location is rows*cols*depth*spaceandtime*otherdimension*sizeof(data_being_stored) -1.
let me see if I can restate that part again:
you CAN NOT DO IT if the memory is not one block of bytes.
Vectors and C arrays ensure this to be true. Linked lists, trees,
or C style arrays allocated via double or triple pointers DO NOT
ensure that. If you try to flatten arrays created from MORE
THAN ONE pointer, it will have access violations and either
crash or do something awful.
|
vectors have one pointer each, so vectors of vectors is just like double pointer and you canna flatten that.
Here are some reason to flatten / do this sort of stuff.
- you need to touch every element, its possible depending on compiler smarts that its faster to have 1 loop instead of nested loops to do that. The difference is probably microscopic on most data on modern hardware and honestly, if you had really big data, you would use threading anyway.
- you want to 'reshape' (stolen from a matlab function that does exactly that) a matrix. You want your 10x10 to really be a 2x50. Both multiply out to 100, so that is OK... but its really hard to do if you allocated it 2d to begin with..! Speaking of transpose, to transpose 'in place' you need to reshape it as well..!