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.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: How to speed up a nested loop? by BrowserUk
in thread How to speed up a nested loop? by Xenofur

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.