Thanks for your help!
I am sorry I missed something important in the code snippet.
 for($i = 0; $i < @lines; $i++)
{
     $sum = 0;
     $sum += $lines[$_]->{foo_value} for ($i - $n + 1 .. $i);
     push @res, $sum;
}
this is what I intend.I try to make it clear and removed something so that
change the original intend without noticing it, sorry.
I write a little program:

#!/usr/bin/perl
use warnings;
use List::Util qw(sum);
push @lines, {foo_value => 6000 + int rand(4000)} for 0 .. 60000;
my $n = 100;
for my $i (99 .. 60000)
{
    my ($sum1, $sum2, $sum3);
    $sum1 += $lines[$_]->{foo_value} for ($i - $n + 1 .. $i);
    map {$sum2 += $_->{foo_value}} @lines[$i - $n + 1 .. $i];
    $sum3 = sum (map {$_->{foo_value}} @lines[$i - $n + 1 .. $i]);
}

and profile it, it turns out the result
3.00s $sum1 += $lines[$_]->{foo_value} for ($i - $n + 1 .. $i);
1.71s map {$sum2 += $_->{foo_value}} @lines[$i - $n + 1 .. $i];
2.10s $sum3 = sum (map {$_->{foo_value}} @lines[$i - $n + 1 .. $i]);

using map on sliced list makes it better but still not acceptable.Any more
ideas?

and I used to think the "foreach $i (0..$#lines)" and "for($i = 0; $i <
@lines; $i++)" are the same.
but the prior cost 66µs , a great victory compared to the second's 50ms.
how should I suppose to know they have efficiency  differences?  *:-)*
Thanks for your help again!

Reply via email to