Hello Monks,

I'm currently doing something that many may look at as being rather silly, but which i'm doing this way as I don't have enough time to do it in a lower level language: I'm writing a 3D OpenGL application in Perl.

The actual 3D part is doing pretty well, however what i'm having problems with is the part that generates the data to be displayed. Due to the fact that the data is pulled directly from the memory space of a different process, i have to pull quite large fragments and have to do that regularly, which results in noticeable skipping in performance.

I can live with that, however would like to minimize it, if possible. I've pinpointed the following part with "use Benchmark;" as the part that uses the most cpu power and now ask you monks whether any of you can see a part that could be improved efficiency-wise:

# cycle through 16 x and 16 y values, # which generate a total of 256 tile indexes for $y ( 0..15 ) { for $x ( 0..15 ) { # this calculates the tile index we are currently at, # using the x and y coords in this block $tile_index = $y+($x*16); # this calculates the real x and y values of this tile $rx = ($bx*16)+$x; $ry = ($by*16)+$y; # insert type data into the internal map array and # note that there was change in this block if applicable if ( !defined $tiles[$rx][$ry][$bz][type] || $tiles[$rx][$ry][$bz][type] != $type_data[$tile_index] ) { $changed = 1; $tiles[$rx][$ry][$bz][type] = $type_data[$tile_index]; } } }
For those of you who wish to see the whole program, it is available on Google Code with the relevant bit being in sub new_process_block.

Thanks in advance for anyone willing to spend some time on this.


Thanks to everyone who pitched in, this is for now the most effective solution over the original, with an improvement of roughly 60%:
my ($rx,$ry,$y,$x,$tile); my $bxScaled = $bx * 16; my $byScaled = $by * 16; my $tile_index=0; for $x ( 0..15 ) { $rx = $bxScaled+$x; $tile = \$tiles[$bz][type][$rx]; for $y ( 0..15 ) { $ry = $byScaled+$y; if ( !defined $$tile->[$ry] || $$tile->[$ry] != $type_data[$tile_index] ) { $changed = 1; $$tile->[$ry] = $type_data[$tile_index]; } ++$tile_index; } }

In reply to 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.