in reply to Re: How to speed up a nested loop?
in thread How to speed up a nested loop?
Incidentally I'm puzzled at the array access of type. Should that be a $type? Or perhaps a hash access? Either way the current version looks like a bug.my $bxScaled = $bx * 16; my $byScaled = $by * 16; for my $x (0 .. 15) { my $xScaled = 16 * $x; # These calculations were factored out of the inner loop my $rx = $bxScaled + $x; my $tiles_for_rx = $tiles[$rx]; for my $y (0 .. 15) { # this calculates the tile index we are currently at, # using the x and y coords in this block my $tile_index = $y + $xScaled; # this calculates the real y value of this tile my $ry = $byScaled + $y; # insert type data into the internal map array and # note that there was change in this block if applicable my $tile = $tiles_for_rx->[$ry][$bz][type]; if (!defined $tile || $$tile != $type_data[$tile_index]) { $changed = 1; $$tile = $type_data[$tile_index]; } } }
Incidentally another cause of your win over the original is that you're using lexical variables consistently, which I believe are slightly faster than package variables.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How to speed up a nested loop?
by GrandFather (Saint) on Sep 17, 2008 at 23:14 UTC | |
by Xenofur (Monk) on Sep 18, 2008 at 08:28 UTC | |
|
Re^3: How to speed up a nested loop?
by Xenofur (Monk) on Sep 18, 2008 at 08:26 UTC | |
by tilly (Archbishop) on Sep 18, 2008 at 15:59 UTC | |
by Xenofur (Monk) on Sep 18, 2008 at 16:19 UTC | |
by kyle (Abbot) on Sep 18, 2008 at 16:32 UTC | |
by Xenofur (Monk) on Sep 18, 2008 at 16:52 UTC | |
|