PHP: Myths and Truths About Performance. Part II
In the previous article I compiled, from my perspective, the most classic myths and truths about the performance of PHP functions and language constructs that I have heard or seen so far. This article will cover several examples with constructs where the performance (or lack thereof) is not always obvious. For example, did you know that incrementing a local variable inside a function will be ~2x faster than a global one?
In the previous article I compiled, from my perspective, the most classic myths and truths about the performance of PHP functions and language constructs that I have heard or seen so far. This article will cover several examples with constructs where the performance (or lack thereof) is not always obvious. For example, did you know that incrementing a local variable inside a function will be ~2x faster than a global one?
$var = 'tests' will be faster than $var = "tests", because PHP will look for variables inside "...", but not inside '...'. Though in recent versions this difference is no longer as pronounced.
a) echo "one", "two", "three" will be faster than echo "one" . "two" . "three". That is, string concatenation will be slower than outputting its fragments.
b) ($x=0; $x < count($array); $x) - the function count will be called at every step of the loop. For example, it can be used like this:
for ($j = 0, $max = count($array); $j < $max; $j++);
c) PCRE regex will be faster than EREG;
d) $str = 'This is a string';
if (isset($str[1])) will be faster than if (strlen($str)>1))
e) Incrementing a local variable (in functions) will be ~2 times faster than a global one;
f) $this->prop++ will be ~3 times slower than a local variable (i.e. $prop++);
g) Incrementing an undefined variable will be ~8 times slower than a predefined one;
h) $obj =& new SomeClass() will be faster than $obj = new SomeClass(), while $obj =& $someClass->f() will be 2 times slower than $obj = $someClass->f();
i) while(list($key) = each($aHash)) $aHash[$key] .= "a" will be faster than foreach($aHash as $key=>$val) $aHash[$key] .= "a";
j) is_array() for a defined value will be 2–3 times faster than is_array() for an undefined value. Therefore it would be more correct to use: if (isSet($foo) AND is_array($foo));
k) Using native constructs will be faster:
// will be faster
$data = file_get_contents("some_file");
// than:
$data = '';
$fp = fopen("some_file", "r");
while ($fp && !feof($fp)) {
$data .= fread($fp, 1024);
} fclose($fp);
More can be read here:
http://www.hm2k.com/posts/50-php-optimisation-tips-revisited
In conclusion I wanted to once again remind you that these examples should not be taken as absolute truth. All the more so because the performance of one function or construct versus another may vary depending on the PHP version, test environment and method of testing. The examples covered here have mostly been "gathered" across the web, and only those for which I found confirmation on more than one website or forum.
It is worth remembering that performance and universality (as well as code readability) are sometimes inversely proportional quantities. The applicability of an alternative construct to the specific task at hand must also be weighed.
comments