I am using a nested loop b/c I have external number of things that need to happen, then an internal number of things that corresponds directly to the external, then a super internal set of things that needs to correspond directly to the internal things.

i.e. MotherA -> ChildA -> genes within childA; MotherA -> ChildB -> genes within childB; MotherB -> ChildB -> genes within childB

I have a list of mothers and fathers that I am randomly combining by saying: 30 mothers (external) have "n" children (internal) from "n" fathers. The children then get n genes from father and n genes from mother (super internal).

I have a code that works exactly as I would like it to. However, the problem is when the number of mothers, and/or number of offspring per mother (think plants), and/or the number of genes I am tracking gets exceedingly large the run time of the script gets horrendously long. Here is a mock up of the code:

use strict; use warnings; my $num_of_external = 30; #num moms my @num_of_internal = (30, 40, 20, 30, 40, 30, 40, 20, 30, 40, 30, 40, 20, 30, 40, 30, 40, 20, 30, 40, 30, 40, 20, 30, 40, 30, 40, 20, 30, 40); #num offspring per mom my $num_of_super_internal = 10; #num genes tracked #during shuffle 5 come from dad 5 come from mom for (1 .. $num_of_external) { my $internal = shift @num_of_internal; #additional things here. #i.e. select mother from possible pool for (1 .. $internal) { #select father for each offspring #additional things here. for (1 .. $num_of_super_internal) { #additional things here. #select genes and shuffle around between mom and dad. } } }
Having gotten this far on my own, I am not sure how to avoid/do away with/restructure/alter/fix this nested loop structure. Is there any way to increase run time by changing the way I am thinking about loops?

Anything you monks can offer would be greatly appreciated by my overheating processor.

Bio

---- Even a blind squirrel finds a nut sometimes.

In reply to Nested loops of doom by BioNrd

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.