in reply to Re^10: How to speed up a nested loop?
in thread How to speed up a nested loop?

Umm, the look-up was already factored out, only with a different syntax, ie.
$stuff = \$stuff2[2]; loop { $moo = $$stuff->[3]; }
instead of
$stuff = $stuff2[2] || []; loop { $moo = $stuff->[3]; }
Or i'm misunderstanding something here. :)

Replies are listed 'Best First'.
Re^12: How to speed up a nested loop?
by tilly (Archbishop) on Sep 23, 2008 at 05:40 UTC
    I think you're misunderstanding. What you had before calculated $rx in the loop, and then had:
    ...$tiles[$rx][$ry]...
    in the inner loop. Which looks up index $rx, gets an array reference, looks up index $ry, etc.

    I moved calculating $rx out of the loop and the lookup on $rx out of the loop with:

    my $tiles_for_rx = ($tiles[$rx] ||= []);
    and then in the loop had:
    ...$tiles_for_rx->[$ry]...
    which means that the lookup of index $rx doesn't happen in the inner loop any more.

    If your inner loop executes 15 times for each iteration of the outer loop, you just lost 14 calculations of $rx, and 14 lookups on index $rx. Less work is generally faster.