title: PHP: understanding how branch predication works
body: <p>After reading <a href="https://en.wikipedia.org/wiki/Branch_predication" rel="nofollow noreferrer">branch predication</a>, I tried to understand how it works.</p>
  
  <p>I wrote a basic test (below). </p>
  
  <p>I think after the first loop php knows how array is structured and predicts the result of condition <code>($items[$i] &lt;= 150000)</code>, but the time of second loop is the same as the first loop and it does not seem to benefit from branch predication.</p>
  
  <p>Some questions:</p>
  
  <ol>
  <li>Maybe php does not support branch predication?</li>
  <li>Maybe I did not understand branch predication? </li>
  <li>My test is wrong/has other performance issues?</li>
  </ol>
  
  <p>My test:</p>
  
  <pre><code>$count = 300000;
  $items = array();
  $found = 0;
  
  // build array
  for ($i = 0; $i &lt;= $count; $i++) {
      array_push($items, rand(0, $i));
  }
  
  // first loop with no predication benefit
  // ------------------------------------
  $time_start = microtime(true);
  
  for($i = 0; $i &lt;= $count; $i++) {
      $found = ($items[$i] &lt;= 150000) ? $found+1 : $found;
  }
  
  $time_end = microtime(true);
  $time = $time_end - $time_start;
  // ------------------------------------
  
  echo "first loop\r\n";
  echo 'Found:'. $found . "\r\n";
  echo 'End:'  . $time  . "\r\n";
  
  // reset counter
  $found = 0;
  
  // second loop with predication benefit
  // ------------------------------------
  $time_start = microtime(true);
  
  for($i = 0; $i &lt;= $count; $i++) {
      $found = ($items[$i] &lt;= 150000) ? $found+1 : $found;
  }
  
  $time_end = microtime(true);
  $time = $time_end - $time_start;
  // ------------------------------------
  
  echo "second loop\r\n";
  echo 'Found:'. $found . "\r\n";
  echo 'End:'  . $time  . "\r\n";
  </code></pre>
  
  <p>output </p>
  
  <pre><code>first loop
  Found:254052
  End:0.093052864074707
  second loop
  Found:254052
  End:0.092923879623413
  </code></pre>
  
  <p><a href="http://codepad.org/Zni0b5rS" rel="nofollow noreferrer">http://codepad.org/Zni0b5rS</a></p>