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.

In reply to Re^12: Parallel::ForkManager is time consuming...takes too long by BrowserUk
in thread Parallel::ForkManager is time consuming...takes too long by esolkc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.