A note on performance:

Two optimizations are needed for "Perl-style" for my $i (reverse 0..$#a) to be as fast as the "C-style" for loop: 1) The elimination of reverse by processing the list backwards, and 2) the treatment of 0..$#a as an interator instead of flattening it. Not all versions of perl implement both of these implementations. As such, it's slower in some versions. (Update: 5.8.6 added (1). (2) still missing when reverse is used. c.f. Re^4: the '..' operator and decreasing values)

use strict; use warnings; use Benchmark qw( cmpthese ); my @a = map { int rand 10 } 1..100; my @b = map { int rand 10 } 1..100; sub perl_for { my $dummy_load; for my $i (reverse 0..$#a) { $dummy_load = $a[$i] . ' ' . $b[$i]; } 1; } sub c_style_for { my $dummy_load; for (my $i=@a; $i--; ) { $dummy_load = $a[$i] . ' ' . $b[$i]; } 1; } sub while_loop { my $dummy_load; my $i = @a; while ($i--) { $dummy_load = $a[$i] . ' ' . $b[$i]; } 1; } sub ickyb0d { my $dummy_load; for my $i (0..$#a) { $dummy_load = $a[$#a-$i] . ' ' . $b[$#a-$i]; } 1; } sub ickyb0d_ { my $dummy_load; for (0..$#a) { my $i = $#a-$_; $dummy_load = $a[$i] . ' ' . $b[$i]; } 1; } sub diotalevi { my $dummy_load; for my $i (1..@a) { $dummy_load = $a[-$i] . ' ' . $b[-$i]; } 1; } cmpthese(-3, { perl_for => \&perl_for, c_style_for => \&c_style_for, while_loop => \&while_loop, ickyb0d => \&ickyb0d, ickyb0d_ => \&ickyb0d_, diotalevi => \&diotalevi, });
This is perl, v5.6.1 built for MSWin32-x86-multi-thread Rate ickyb0d ickyb0d_ perl_for diotalevi while_loop c +_style_for ickyb0d 2516/s -- -8% -44% -48% -52% + -52% ickyb0d_ 2726/s 8% -- -40% -43% -48% + -48% perl_for 4507/s 79% 65% -- -6% -14% + -14% diotalevi 4794/s 91% 76% 6% -- -9% + -9% while_loop 5261/s 109% 93% 17% 10% -- + -0% c_style_for 5261/s 109% 93% 17% 10% 0% + --

Still, the Perl-style for loop is not much slower (14% slower, maybe less for smaller arrays) than the C-style for loop even in "old" version 5.6.1.

Update: Added diotalevi's solution.


In reply to Re: Looping backwards through two arrays at once? by ikegami
in thread Looping backwards through two arrays at once? by Spidy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.