I can't imagine the following refactoring will make much difference, but it may help a little:
my $bxScaled = $bx * 16; my $byScaled = $by * 16; for my $x (0 .. 15) { my $xScaled = 16 * $x; 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 x and y values of this tile my $rx = $bxScaled + $x; 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[$rx][$ry][$bz][type]; if (!defined $tile || $$tile != $type_data[$tile_index]) { $changed = 1; $$tile = $type_data[$tile_index]; } } }
Main changes are to reduce the number of multiplies performed in the inner loop and to reduce the number of array indexing operations.
Update: A quick benchmark indicates about a 40% speed improvement. I'm surprised!
use strict; use warnings; use Benchmark qw(cmpthese); use constant type => 0; cmpthese (-1, { org => 'org ()', gf => 'gf ()', }); sub org { my $bz = 0; my $bx = 0; my $by = 0; my $changed; my @tiles; my @type_data; for my $y (0 .. 15) { for my $x (0 .. 15) { # this calculates the tile index we are currently at, # using the x and y coords in this block my $tile_index = $y + ($x * 16); # this calculates the real x and y values of this tile my $rx = ($bx * 16) + $x; my $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_ind +ex]) { $changed = 1; $tiles[$rx][$ry][$bz][type] = $type_data[$tile_index]; } } } } sub gf { my $bz = 0; my $bx = 0; my $by = 0; my $changed; my @tiles; my @type_data; my $bxScaled = $bx * 16; my $byScaled = $by * 16; for my $x (0 .. 15) { my $xScaled = 16 * $x; 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 x and y values of this tile my $rx = $bxScaled + $x; 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[$rx][$ry][$bz][type]; if (!defined $tile || $$tile != $type_data[$tile_index]) { $changed = 1; $$tile = $type_data[$tile_index]; } } } } __END__ Rate org gf org 1372/s -- -29% gf 1939/s 41% --
Update: a further small improvement (about 10%) is realized by moving the $rx calculation to the outer loop.
Adding my $xTiles = $tiles[$rx] to the outer loop and using my $xTiles in place of $tiles[$rx] in the inner loop gives another 2% improvement (although that's getting to be in the benchmark noise and may be bogus).
In reply to Re: How to speed up a nested loop?
by GrandFather
in thread How to speed up a nested loop?
by Xenofur
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |