I completely reworked the solution using two vectors: one for the units on the front panel and one for the other blocks, and changed all the algorithms so that the block with the changed status immediately moves from one vector to another. Thus, I eliminated counting in the operator [], which was the main bottleneck.
Before using the profiler, I get the calculation time from 5500 to 7000 ms. After looking at the answers here, 1) I changed the loop variables from ushort to int or uint, which reduced the duration by ~ 10%, 2) I made another modification in the secondary algorithm to reduce the duration by another 30% or so, 3) I implemented two vectors, as explained above. This helped reduce the computation time from ~ 3300 ms to ~ 700 ms, another 40%!
In all this, a reduction of 85 - 90%! Thanks SO and profiler.
Next, I’m going to implement an intermediary template and only if necessary call the update function, possibly by calling a few more ms. :)
New code corresponding to the old fragment (now the functionality is completely different):
UnitBase* Formation::operator[](ushort offset) { if (offset < numFightingUnits) return unitFormation[offset]->getUnit(); else return NULL; }
Much shorter and bigger. Of course, there were many other heavy modifications, the most important of which is that unitFormation is now std::vector<UnitFormationElement*> , and not just UnitBase** . UnitFormationElement* contains UnitBase* along with some other vital data that used to hang in the Formation class.
source share