While I don't know how much of the difference between the perl and c code this makes up, after a cursory scan I do see a number of inefficiencies in the perl code.
The first thing that jumps out at me are all the
my calls inside the loops. If you are going to be declaring these variables so many times, it's more efficient to do so outside the loops. Here's a quick benchmark:
Benchmark::cmpthese(5000, {
'outside' => sub {
my $x; my $y; my @z; my $i;
for ($i = 0; $i < 1000; $i++) {
$x = $i;
$y = $x;
@z = ($x, $y)
}
},
'inside' => sub {
for (my $i=0; $i < 1000; $i++) {
my $x = $i;
my $y = $x;
my @z = ($x, $y)}
}
});
Rate inside outside
inside 120/s -- -13%
outside 138/s 15% --
So in this example, declaring the
my variables outside of the loop gives a 15% speed up.
Another problem I found was calculating the loop limiting condition in the
for loop. e.g.,
for(my $i = 64; $i < (scalar(@rddt) - (2 * $pond)); $i++){
Since it doesn't look like the size of @rddt or the value of $pond is changing, you should do that calculation outside of the loop.
Here are the benchmarks:
@rddt = (1) x 1000;
$pond = 32;
Benchmark::cmpthese(500, {
'inside' => sub {
for ( my $i = 64 ; $i < ( scalar(@rddt) - ( 2 * $pond ) )
+; $i++ ) {
$x++;
}
},
'outside' => sub {
$limit = scalar(@rddt) - ( 2 * $pond );
for ( my $i = 64 ; $i < $limit ; $i++ ) {
$x++;
}
},
});
inside 273/s -- -39%
outside 450/s 65% --
A 65% speed up here. There are probably lots of other optimizations that you could do in this perl code. Those two were the most obvious.