I have tonnes of such subroutines and the entire code takes 1 minute and 35 seconds.
Hm. 95 / 0.609 implies that "tonnes" == 156 subroutines?
If that is really the case, then recoding them all similar to this:
use List::Util qw[ sum ];
...
sub subroutine {
our( @a, @b, @c, @d, @e );
( *a, *b, *c, *d, *e ) = @_;
foreach my $i ( 10 .. $#a ) {
$c[$i] = $d[$i] = $e[$i] = 0;
my $b = $b[$i];
if( ( $b >= 5 ) && ( $b < 7 ) ) {
$c[$i] += sum @a[ $i-$b+1 .. $i ];
$c[$i] /= $b;
}
elsif( ( $b >= 7 ) && ( $b < 9 ) ) {
$d[$i] += sum @a[ $i-$b+1 .. $i ];
$d[$i] /= $b;
}
elsif( ( $b >= 9 ) && ( $b < 15 ) ) {
$e[$i] += sum @a[ $i-$b+1 .. $i ];
$e[$i] /= $b;
}
}
return;
}
## called like this
my @a = ...;
my @b = ...;
my( @c, @d, @e );
subroutine( \@a, \@b, \@c, \@d, \@e );
which runs in 1/3rd the time of your original, should get you close to your target. If not, then you'd have to sort out the inefficiencies in the rest of your code as well.
If it is really going to be necessary to multi-process this to achieve your target, then you should not be trying to inject the forks or threads into the loops within these subroutines -- as in your OP code-- but rather call the subroutines themselves concurrently. To advise you further on doing that, I'd need to see the calling code in its entirety.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|