in reply to Re^6: Parallel::ForkManager is time consuming...takes too long
in thread Parallel::ForkManager is time consuming...takes too long

the objective is to get a fast code

If you are trying to improve performance why are you duplicating whole arrays inside your subroutine when they are only referenced read-only?

my (@arraya,) = @$listaref; my (@arrayb) = @$listbref;

THIS MAKE NO SENSE!

Let me try it again.

Your code snippet STILL makes no sense.

You loop over the indices of arrayA:

foreach my $i (0 .. $#arraya) {

Then, in each iteration, you compare the highest index of arrayA $#arraya with two "reference values":

if(($#arraya >= $someReferenceValue1) && ($#arraya < $someReferenceVal +ue2))

None of these 3 values change anywhere in the loop, so you are performing the same exacts set of tests over and over, and you will take the same action over and over. No wonder your code is slow. No wonder your code is slow. No wonder your code is slow. No wonder your code is slow. No wonder your code is slow. No wonder your code is slow. No wonder your code is slow. Repeating the same tests over and over is pointless.

YOUR CODE MAKES NO SENSE! If you want help, don't post garbage 'minds-eye' pseudo-code.

You said above that my code was "faster than your implementation". So, post "your implementation" -- ie. runnable code -- and maybe someone will try to help you.


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.

Replies are listed 'Best First'.
Re^8: Parallel::ForkManager is time consuming...takes too long
by esolkc (Acolyte) on Aug 16, 2011 at 15:32 UTC
    I am sorry for this confusion. I posted now an excecutable script as an example (see above). Array_b provides the information of the time lag for determining a simple moving average. The subroutine is supposed to give three arrays back. Does that makes sense now? Anyhow, something is wrong with it and basically I have issues to get the three arrays into my main program. Finally, is there a way for threading the calculations of such a subroutine?
      The code had still some bugs. Now it works.
      #!/usr/bin/perl @array_a = (66.93, 66.91, 66.84, 66.82, 66.87, 66.91, 66.7, 66.8, 66.5 +7, 66.61, 66.81, 66.72, 66.78, 66.76, 66.86, 66.73, 66.82, 66.95, 66. +88, 66.85, 66.87, 66.75, 66.88, 66.93, 66.94, 66.87, 66.88, 66.85, 66 +.83, 66.89, 66.96, 67.03, 67.08, 67.22, 67.36, 67.55, 67.5, 67.49, 67 +.38, 67.49, 67.47, 67.7, 67.62, 67.58, 67.61, 67.79, 67.73, 67.79, 67 +.81, 67.94, 67.99, 68.18, 68.01, 67.82, 67.71, 67.71, 67.65, 67.69, 6 +7.7, 67.7, 67.69, 67.66, 67.62, 67.58, 67.56, 67.5, 67.24, 67.36, 67. +45, 67.46, 67.38, 67.55, 67.53, 67.56, 67.67, 67.78, 67.78, 67.78, 67 +.78, 67.84, 67.78, 67.74, 67.9, 67.72, 67.74, 67.79, 67.86, 67.88, 67 +.93, 67.97, 68.09, 67.76, 67.89, 67.81, 67.97, 68, 67.84, 67.95, 67.9 +7, 68.02, 68.07, 68.03, 68.08, 68.05, 68.05, 68.07, 68.14, 68.18, 68. +21, 68.13, 68.07, 68.02, 68, 67.96); @array_b = (10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 9, 9, 9 +, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, +10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 9, 9, 9, 9, 9, 8, 8, 8 +, 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7 +, 8, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, +10, ); my ($array_ref_c, $array_ref_d, $array_ref_e) = subroutine(\@array_a,\ +@array_b); my @array_c = @$array_ref_c; my @array_d = @$array_ref_d; my @array_e = @$array_ref_e; foreach my $i (0 .. $#array_a) { print($i, "\t",$array_b[$i],"\t",$array_a[$i],"\t",$array_c[$i],"\t" +,$array_d[$i],"\t",$array_e[$i],"\n"); } sub subroutine { my ($listaref, $listbref) = @_; my (@array_a) = @$listaref; my (@array_b) = @$listbref; foreach my $i (10 .. $#array_a) { $array_c[$i] = 0; $array_d[$i] = 0; $array_e[$i] = 0; if (( $array_b[$i] >= 5 ) && ( $array_b[$i] < 7 )) { for ( $l = 0 ; $l < $array_b[$i] ; $l++ ) { $array_c[$i] = $array_c[$i] + $array_a[$i - $l]; } $array_c[$i] = $array_c[$i] / $array_b[$i]; } if (( $array_b[$i] >= 7 ) && ( $array_b[$i] < 9 )) { for ( $l = 0 ; $l < $array_b[$i] ; $l++ ) { $array_d[$i]= $array_d[$i] + $array_a[$i - $l]; } $array_d[$i] = $array_d[$i] / $array_b[$i]; } if (( $array_b[$i] >= 9 ) && ( $array_b[$i] < 15 )) { for ( $l = 0 ; $l < $array_b[$i] ; $l++ ) { $array_e[$i]= $array_e[$i] + $array_a[$i - $l]; } $array_e[$i] = $array_e[$i] / $array_b[$i]; } #print($i, "\t",$array_b[$i],"\t",$array_a[$i],"\t",$array_c[$i],"\t +",$array_d[$i],"\t",$array_e[$i],"\n"); } return (\@array_c, \@array_d, \@array_e); } __END__

        I just ran the above code with arrays A & B containing 100,000 values each. It completed in a little over half a second

        C:\test>920533 >920533.obig Took 0.609000 seconds C:\test>wc -l 920533.obig 100000 920533.obig

        What made you think you needed to use multiprocessing to meet your 30 second target time?


        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.