title: Why are PHP function calls *so* expensive?
body: <p>A function call in PHP is expensive. Here is a small benchmark to test it:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  const RUNS = 1000000;
  
  // create test string
  $string = str_repeat('a', 1000);
  $maxChars = 500;
  
  // with function call
  $start = microtime(true);
  for ($i = 0; $i &lt; RUNS; ++$i) {
      strlen($string) &lt;= $maxChars;
  }
  echo 'with function call: ', microtime(true) - $start, "\n";
  
  // without function call
  $start = microtime(true);
  for ($i = 0; $i &lt; RUNS; ++$i) {
      !isset($string[$maxChars]);
  }
  echo 'without function call: ', microtime(true) - $start;
  </code></pre>
  
  <p>This tests a functionally identical code using a function first (<code>strlen</code>) and then without using a function (<code>isset</code> isn't a function).</p>
  
  <p>I get the following output:</p>
  
  <pre><code>with function call:    4.5108239650726
  without function call: 0.84017300605774
  </code></pre>
  
  <p>As you can see the implementation using a function call is more than five (5.38) times slower than the implementation not calling any function.</p>
  
  <p>I would like to know why a function call is so expensive. What's the main bottleneck? Is it the lookup in the hash table? Or what is so slow?</p>
  
  <hr>
  
  <p>I revisited this question, and decided to run the benchmark again, with XDebug completely disabled (not just profiling disabled). This showed, that my tests were fairly convoluted, this time, with 10000000 runs I got:</p>
  
  <pre><code>with function call:    3.152988910675
  without function call: 1.4107749462128
  </code></pre>
  
  <p>Here a function call only is approximately twice (2.23) as slow, so the difference is by far smaller.</p>
  
  <hr>
  
  <p>I just tested the above code on a PHP 5.4.0 snapshot and got the following results:</p>
  
  <pre><code>with function call:    2.3795559406281
  without function call: 0.90840601921082
  </code></pre>
  
  <p>Here the difference got slightly bigger again (2.62). (But on the over hand the execution time of both methods dropped quite significantly).</p>