in reply to Re^3: Some code optimization
in thread Some code optimization
Let's look at a single iteration and define n as the number of genes. A gene is a start point + length, i.e. n is the number of elements pushed in the creation of $simulation_h (i.e. n=70,000 in the example).
The main loop is indeed a nested loop, but it simply gets a single gene at a time from the hash, hence this double loop itself takes O(n) (before doing anything).
Now, let's look at what is done inside the loop. We call two "problematic" subroutines, which in turn, call some other subroutines. The "loops" you see in the other subroutines are used to deal with the case when genes are split (start too close to the "end" of a circular the chromosome, thus have to be represented by two ranges instead of one). A range can therefore be either a singleton (one array element) or "split" (two array elements).
So, yes, there are loops, but they sure don't add another factor of n to the complexity, since these loops are bounded by a small constant -- each loop is iterated for maximum of 2 iterations -- so even the double loop that checks a pair of ranges against each other (each range is "split" in the worse case scenario) is iterated for maximum of 4 iterations.
To conclude, the complexity is actually O(n) per iteration (again, n is the number of genes).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Some code optimization
by davido (Cardinal) on Jun 18, 2010 at 08:52 UTC | |
by roibrodo (Sexton) on Jun 18, 2010 at 09:11 UTC |