Apply small optimizations.
[genetic.git] / src / algorithm.h
index 67b7160ef0580b0407d99f0883ecca5c22a1ae87..bc928506b08846567ad6ab81dba4dc454e909269 100644 (file)
@@ -119,9 +119,12 @@ namespace genetic {
          * \attention Method is currently not in use.
          */
         void showGeneration() {
-            for (unsigned int i = 0; i < this->generation.size(); i++) {
+            unsigned int generationSize = this->generation.size();
+            unsigned int chromosomeSize = this->generation[0].size();
+
+            for (unsigned int i = 0; i < generationSize; i++) {
                 cout << "# " << i << ") ";
-                for (unsigned int j = 0; j < this->generation[i].size(); j++) {
+                for (unsigned int j = 0; j < chromosomeSize; j++) {
                     cout << this->generation[i][j].get();
                 }
                 cout << "\n";
@@ -133,12 +136,14 @@ namespace genetic {
          */
         void showAvgFitness() {
             double avg = 0;
-            for (unsigned int i = 0; i < this->generation.size(); i++) {
+            unsigned int generationSize = this->generation.size();
+
+            for (unsigned int i = 0; i < generationSize; i++) {
                 _Fitness fit(this->generation[i]);
                 fit.parseArguments(fitness.getArguments());
                 avg += fit.calculate();
             }
-            cout << " " << avg / this->generation.size();
+            cout << " " << avg / generationSize;
         }
 
         /**
@@ -146,7 +151,9 @@ namespace genetic {
          */
         void showBestFitness() {
             double best = -100000;
-            for (unsigned int i = 0; i < this->generation.size(); i++) {
+            unsigned int generationSize = this->generation.size();
+
+            for (unsigned int i = 0; i < generationSize; i++) {
                 _Fitness fit(this->generation[i]);
                 fit.parseArguments(fitness.getArguments());