Is
$output[int($_/3)] += $arr[$_] for 0..@arr-1;
efficient enough, or is there something I am missing? Too many "int()"s? My guess is that
my $k = @arr / 3;
for (0..$k) {
$output[$_] += $arr[3*$_] + $arr[3*$_+1] + $arr[3*$_ +2];
}
is about as efficient as you'd get.
Edit
Actually, it seems to me that
my $k = @arr / 3;
my $b = 0;
for (0..$k) {
$output[$_] = $arr[$b] + $arr[$b+1] + $arr[$b +2];
$b += 3;
}
is probably a tad faster. Although if your problem really is as big as to make a difference between those 2 versions, then you shouldn't really be using Perl.