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

Oops, my bad. I didn't know what datastructure you had so I hadn't realized that you were autovivifying there. Make that line:
my $tiles_for_rx = ($tiles[$rx] ||= []);
instead and see if it works better.

Replies are listed 'Best First'.
Re^9: How to speed up a nested loop?
by Xenofur (Monk) on Sep 19, 2008 at 09:42 UTC
    Wow, thanks! This actually gave a little performance boost.

    I guess this is caused by reducing the double-resolution of "$$tile->[]" to a single resolution of "$tile->[]" that this allows.
      Nope. It is caused by factoring yet another lookup out of the inner loop.

      In languages like C this kind of optimization is meaningless because the compiler is smart enough to know when to do it for you. But Perl has to make a trade-off between compiling well and runtime performance, so doesn't make this trade-off.

      If Perl had a JIT compiler then this would go away as well. The Parrot folks have promised a JIT compiler for Perl 6, but I've stopped holding my breath on waiting for that to happen.

        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. :)