A function call in PHP is expensive. Here is a small benchmark to test it:
<?php
function stringOkay($string, $maxChars) {
return !isset($string[$maxChars]);
}
const RUNS = 1000000;
// create test string
$string = str_repeat('a', 1000);
$maxChars = 500;
// userland function call
$start = microtime(true);
for ($i = 0; $i < RUNS; ++$i) {
stringOkay($string, $maxChars);
}
echo 'userland function call: ', microtime(true) - $start, '<br />';
// native function call
$start = microtime(true);
for ($i = 0; $i < RUNS; ++$i) {
strlen($string) <= $maxChars;
}
echo 'native function call: ', microtime(true) - $start, '<br />';
// userland implementation
$start = microtime(true);
for ($i = 0; $i < RUNS; ++$i) {
!isset($string[$maxChars]);
}
echo 'userland implementation: ', microtime(true) - $start, '<br />';
This tests a functionally identical code as a userland function, an implementation using a native function and an implementation using no functions at all.
I get the following output (yes, my machine is slow):
userland function call: 5.3892569541931
native function call: 4.5108239650726
userland implementation: 0.84017300605774
As you can see the native function call is more than five times slower than the implementation not calling any function. Now, I know these two codes aren't identical. I do understand that strlen
needs to create a variable and return it, I understand that it probably does some checking of the passed value before proceeding. So, let us assume that strlen
is 30% heavier than isset
. Still the difference is big.
Now, 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?