title: Optimizing PHP code that uses a lot of for loops
body: <p>My PHP code contains a lot of foreach loops and results are absolutely disastrous. It takes way too long to run.</p>
  
  <p>Is there an alternative to this. I am open to all suggestions.</p>
  
  <p>I am thinking may be implementing a flash based client and using actionscript to utilize the client's CPU for running the logic.</p>
  
  <p>Or is there a way to use C/C++ to handle the computationally demanding part on the server itself and return PHP the results.</p>
  
  <p>The following function gets called 1,000,000 times.</p>
  
  <pre><code>public function performEnrichmentAnalysis($geneSet) {
      /**
       * $mainArray is a multi dimentional array.
       * EntrezID | Set (0/1) | pValue | rank
       */
      $mainArray = array();
      $finalArray = array();
      $originalGenesScore = 0;
      $randomGenesScore = 0;
      $u = 0;
      $EntrezID = array();
      $Set = array();
      $pValue = array();
      $Rank = array();
      $originalGenes = $geneSet-&gt;getGenes();
      $memeberCount = $geneSet-&gt;getGeneCount();
      $randomGenes = $this-&gt;geneExpressionData-&gt;getRandomGenes($memeberCount);
      /**
       * Copy the elements of original and random gene sets to main array.
       */
      foreach ($originalGenes as $key =&gt; $value) {
          $pVal = $this-&gt;geneExpressionData-&gt;getExpressionValue($value);
          $array = array('EntrezID' =&gt; $value, 'Set' =&gt; 0, 'pValue' =&gt; $pVal, 'Rank' =&gt; 999);
          array_push($mainArray, $array);
          unset($array);
      }
      foreach ($randomGenes as $key =&gt; $value) {
          $pVal = $this-&gt;geneExpressionData-&gt;getExpressionValue($value);
          $array = array('EntrezID' =&gt; $value, 'Set' =&gt; 1, 'pValue' =&gt; $pVal, 'Rank' =&gt; 999);
          array_push($mainArray, $array);
          unset($array);
      }
      /**
       * sort the multi dimentaional array based on p-values
       */
      foreach ($mainArray as $key =&gt; $row) {
          $EntrezID[$key] = $row['EntrezID'];
          $Set[$key] = $row['Set'];
          $pValue[$key] = $row['pValue'];
          $Rank[$key] = $row['Rank'];
      }
      array_multisort($pValue, SORT_ASC, $mainArray);
  
      /**
       * Assign ranks to the genes
       */
      for ($index = 0; $index &lt; count($mainArray); $index++) {
          $row = $mainArray[$index];
          $row['Rank'] = $index + 1;
          $row['Score'] = 0;
          //print_r($row['Rank']);
          array_push($finalArray, $row);
      }
  
      /**
       * Calculate scores for each gene
       */
      for ($i = 0; $i &lt; count($finalArray); $i++) {
          for ($j = $i + 1; $j &lt; count($finalArray); $j++) {
              if ($finalArray[$j]['Set'] != $finalArray[$i]['Set']) {
                  $finalArray[$i]['Score']++;
              }
          }
      }
  
      /**
       * Calculate score for the entire set and get universal U and z score.
       */
      for ($counter = 0; $counter &lt; count($finalArray); $counter++) {
          if ($finalArray[$counter]['Set'] == 0) {
              $originalGenesScore += $finalArray[$counter]['Score'];
          }
          if ($finalArray[$counter]['Set'] == 1) {
              $randomGenesScore += $finalArray[$counter]['Score'];
          }
      }
  
      if ($originalGenesScore &gt; $randomGenesScore) {
          $u = $randomGenesScore;
      } else {
          $u = $originalGenesScore;
      }
  
      $zNumerator = $u - (($memeberCount * $memeberCount) / (2));
      $zDenominatorSquared = ($memeberCount * $memeberCount * ($memeberCount + $memeberCount + 1)) / 12;
  
      $z = $zNumerator / sqrt($zDenominatorSquared);
  
      if (abs($z) &gt; 2.303) {
          $this-&gt;temp001++;
      } elseif (abs($z) &gt; 1.605) {
          $this-&gt;temp005++;
      } else {
          $this-&gt;tempRemaining++;
      }
  }
  </code></pre>