Chas Owens wrote:
> On 4/20/07, yitzle <[EMAIL PROTECTED]> wrote:
>> There's been mentions of efficiency.
>> I'm under the impression that for the uses Perl is put to, the
>> efficiency of a loop is sorta irrelevent. If you are doing
>> harddrive/network access, the performance gain of one loop over the
>> other is more of less invisible.
>> Come to think of it, the time for interpretting got to overshadow the
>> performance gain...
> 
> I was trying to address all arguments for the use of C-style for.  In
> general, though, premature optimization is a waste of time.  You
> should be aware of the general cost of the constructs you use (that is
> why I occasionally post benchmarks for alternate algorithms), but you
> should be more concerned with making what you write clearly express
> your intent.  This is why I prefer
> 
> my @a = (0 .. 10);
> my $i = 0;
> for my $elem (grep {not $i++ % 3} @a) {
>       func($elem);
> }
> 
> to
> 
> for (my $i = 0; $i < @a; $i += 3) {
>    func($a[$i]
> }
> 
> The grep clearly states that I am looking for something and its block
> tells me what the criteria are.

You are omitting one critical argument.  For people who are stuck with older
versions of Perl and in your grep() example above the foreach expression
creates its list in memory which may cause the program to die if the list is
large enough while the C style for loop does not have this problem.



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to