in reply to How to speed up a nested loop?

You'd be better off if you'd rearrange your data structure so that you could use strings of 256 bytes (or whatever is big enough for what you are assigning) and string bit ops.

$changed = ~$defined | ($type ^ $new); $type = $new; $defined = "\xFF" x 256;

But since you since you provided no information about your data structure — What are $bz, tile and the data you are assining, and why do you add $bx+16 and $by+16 — can't help you beyond that.

Update: Oops, the sample code I provided worked at the bit level, not the byte level. And I can't think of a good way to do it at the byte level at the moment.

Why aren't you using PDL anyway?

Replies are listed 'Best First'.
Re^2: How to speed up a nested loop?
by Xenofur (Monk) on Sep 18, 2008 at 09:26 UTC
    Ok, short explanation about what's going on:

    There's this game that looks like this: http://www.tigsource.com/features/images/dwarffortress-big.png And i'm writing this to get something that looks like this: http://www.videogames.net.au/images/dwarf-fortress-3d-visualizer-beautiful-fortress1.jpg

    The game internally stores the data as 16x16 blocks of single tiles. My script grabs the current cursor position and then grabs the data around the cursor and converts it into a big unified multi-dimensional by extracting the tile type and storing it in said array. That array is then later used to render images in OpenGL.

    $bx and $by are the coordinates of each 16x16 block on the 2d plane, while $bz is the z dimension that's equal for tiles and blocks. I get the data for each block as an array of 256 values, through which i cycle, and which i then insert into the "real" coordinates it would have, which is why i'm doing the ($bx*16)+$x stuff.

    A diagram of the refresh loop is available here: http://dwarvis.googlecode.com/svn/trunk/lifevis/lifevis.dia

    As for your suggestions: I can't even figure out what you're doing with the binary operations there and i'd like to strike a balance between readability and performance. PDL, i have never heard about that and from glancing at the linked page i also can't figure out how it would help me. Sorry if i'm being dumb here and thanks for trying.
      PDL, i have never heard about that and from glancing at the linked page i also can't figure out how it would help me.
      You seem to be doing quite a lot of array calculations, and if I understand it correctly, that's what PDL (pdl.perl.org) is particularly good at:
      PDL ("Perl Data Language") gives standard Perl the ability to compactly store and speedily manipulate the large N-dimensional data arrays.
      Please do check out the demos, screenshots and success stories.
      --
      No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
        Memory is not an issue and its main feature, which is being able to apply functions quickly to the entire contents of an array is not useful in my case. I will however test it a bit to see if it can actually handle the data faster.