in reply to How to speed up a nested loop?
The index mappings produced (fixing $bx & $by as 0) are:
tiles[ 0][ 0][constant][type] = type_data[ 0] tiles[ 1][ 0][constant][type] = type_data[ 16] tiles[ 2][ 0][constant][type] = type_data[ 32] tiles[ 3][ 0][constant][type] = type_data[ 48] tiles[ 4][ 0][constant][type] = type_data[ 64] tiles[ 5][ 0][constant][type] = type_data[ 80] tiles[ 6][ 0][constant][type] = type_data[ 96] tiles[ 7][ 0][constant][type] = type_data[112] tiles[ 8][ 0][constant][type] = type_data[128] tiles[ 9][ 0][constant][type] = type_data[144] tiles[10][ 0][constant][type] = type_data[160] tiles[11][ 0][constant][type] = type_data[176] tiles[12][ 0][constant][type] = type_data[192] tiles[13][ 0][constant][type] = type_data[208] tiles[14][ 0][constant][type] = type_data[224] tiles[15][ 0][constant][type] = type_data[240] ... tiles[ 0][15][constant][type] = type_data[ 15] tiles[ 1][15][constant][type] = type_data[ 31] tiles[ 2][15][constant][type] = type_data[ 47] tiles[ 3][15][constant][type] = type_data[ 63] tiles[ 4][15][constant][type] = type_data[ 79] tiles[ 5][15][constant][type] = type_data[ 95] tiles[ 6][15][constant][type] = type_data[111] tiles[ 7][15][constant][type] = type_data[127] tiles[ 8][15][constant][type] = type_data[143] tiles[ 9][15][constant][type] = type_data[159] tiles[10][15][constant][type] = type_data[175] tiles[11][15][constant][type] = type_data[191] tiles[12][15][constant][type] = type_data[207] tiles[13][15][constant][type] = type_data[223] tiles[14][15][constant][type] = type_data[239] tiles[15][15][constant][type] = type_data[255]
Which means that you are taking a one dimension array of 256 values, treating it as a 2D 16x16 array, and mapping vertical slices of that, onto horizontal slices of your display:
@type_data [00,01,02,03] [10,11,12,13] [20,21,22,23] [30,31,32,33] @tiles bx | +--+--+--+--+--+--+--+--+--+--+-- | | | | | | | | | | | +--+--+--+--+--+--+--+--+--+--+-- | | | | | | | | | | | +--+--+--+--+--+--+--+--+--+--+-- | | | | | | | | | | | +--+--+--+--+--+--+--+--+--+--+-- | | | | | | | | | | | by-+--+--+--+--+--+--+--+--+--+--+-- | | | | |00|10|20|30| | | +--+--+--+--+--+--+--+--+--+--+-- | | | | |01|11|21|31| | | +--+--+--+--+--+--+--+--+--+--+-- | | | | |02|12|22|32| | | +--+--+--+--+--+--+--+--+--+--+-- | | | | |03|13|23|33| | | +--+--+--+--+--+--+--+--+--+--+-- | | | | | | | | | | |
And it is actually much worse than that, because you have two extra layers of indirection at each of those points: $bz & type(???):
As there is no OpenGL premative that can use this 4-deep array structure directly, then at some point later you must be unpacking chunks of it to pass to OpenGL primitives.
At the very least, you should re-order your tiles structure to: $tiles[$bz][$by][$bx][type] which would allow for a far more direct mapping of the elements with far less indirect for any given value of $bz which appears to be a constant during these loops. If you could move the type constant lower in the structure, then you could do the assignments using array slices 16 values at a time.
Basically, to improve your performance much beyond where you are now, you need to seriously re-consider the way you are structuring your data. First the ordering to allow more direct mapping.
Secondly, the complexity. If type is a constant, why is it there? It seems like you're just overstructuring your data and paying for it in time.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to speed up a nested loop?
by Xenofur (Monk) on Sep 18, 2008 at 14:00 UTC | |
by BrowserUk (Patriarch) on Sep 18, 2008 at 14:28 UTC | |
by Xenofur (Monk) on Sep 18, 2008 at 14:49 UTC |