Abigail,

That was an off the cuff remark about using for(;;) instead of for ($lower..$upper). Of course there are uses of for(;;) that do not map on to for ($i..$j). However this wasnt one of them. And for this type of case I stand by my remarks.

Furthermore comparing

for (my $i = 0; $i < 1000000; $i += 2) {$sum += $i}
to
for my $i (map {$_ * 2} 0 .. 1000000 / 2) {$sum += $i}
is a bit strange, when internally it is closer to
{ my $i=0; while ($i < 1000000) { $sum+=$i; } continue { $i+=2; } }
As for all the other languages you listed, none (that I know of) but the C derivatives have this weird form of while loop pretending to be a for loop. For a programmer whose experience is limited to one of the ones without the equivelence the construct is particularly confusing. So from that POV I stand by calling the three arg for loop construct a dirty C trick.

And I stand by the efficiency claims for the case to which I was primarily referring:

use strict; use Benchmark qw(cmpthese); our $sum; cmpthese -10,{ 'foreach' => <<'CODE', 'for(;;)' => <<'MORECODE' }; $::sum=0; for my $i (0..10_000) { $::sum+=$i; } CODE $::sum=0; for (my $i=0;$i<10_000;$i++) { $::sum+=$i; } MORECODE __END__ Benchmark: running for(;;), foreach, each for at least 10 CPU seconds. +.. for(;;): 11 wallclock secs (10.61 usr + 0.00 sys = 10.61 CPU) @ 85 +.30/s (n=905) foreach: 11 wallclock secs (10.63 usr + 0.00 sys = 10.63 CPU) @ 14 +3.15/s (n=1521) Rate for(;;) foreach for(;;) 85.3/s -- -40% foreach 143/s 68% --
Id say thats more efficient wouldnt you?

--- demerphq
my friends call me, usually because I'm late....


In reply to Re: Re: extracting corresponding values from another array by demerphq
in thread extracting corresponding values from another array by Anonymous Monk

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.