I have listed my top tips for writing faster (optimized) PHP code:
echo $variable1 . 'string1' . $variable2 . $variable3;
But, you may be forgetting that echo can take multiple arguments. So you can write it like this:
echo $variable1 , 'string1' , $variable2 , $variable3;
Passing multiple arguments to echo is faster than joining the strings first, and then passing them to echo. I done a little test in PHP. I echoed 10 different strings 500 thousand times, firstly with the concatenation (joining) method, and then with the multiple arguments method. Here are my results:
Time for concatenation method: 37.83755 seconds
Time for multiple arguments method: 37.68789 seconds
Time saved: 0.15966 seconds; 0.396%
As you can see, the difference is very small. It is not worth going over all your old PHP scripts and changing the dots to commas, unless you are extremely desperate for speed. It is more a case of preference, than there being a best way.
Oh, and another small tip (without it's own number): use echo instead of print - it's faster!
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
for($a = 0; $a < sizeof($array); $a++) {
// Do something
}
How many times is the function sizeof() called? If you think once, you are wrong. It is actually called 21 times - once for every iteration. You should do the function call (or calculation) outside of the for loop, assign the value to a variable and use the variable in the for loop.
I done another test. In each test, I had an array with 10,000 elements, and a for loop with 10,000 iterations. In the first test, I had sizeof($array) within the for loop - so it would be called 10,001 times. In the second test, I got the value sizeof($array) and assigned it to the variable $no. I then used $no in the for loop. Test 2 should be faster than test 1, because sizeof() is called 10,000 times less. Here are my results:
Test 1 (sizeof() called 10, 001 times): 0.02784 seconds
Test 2 (sizeof() called 1 time): 0.01278 seconds
Time saved: 0.01506 seconds; 54.095%
$string = 'String for Output';
echo $string;
If you are only going to use $string once, do we need it? No, we just need it's value. If we keep it as a variable, it is going to stay in the memory for the entire script. This would be better:
echo 'String for Output';
If you do need to use a variable, you might want to consider using unset() to destroy it.
$string = 'Hello';
…is better than:
$string = "Hello";
I done a test to see how much difference this makes. In the first test, I assigned a string to a variable 50,000,000 times using double quotes. In the second test, I assign a string to a variable 50,000,000 times, but this time with single quotes. Here are the results of my test:
Double Quotes: 51.74447 seconds
Single Quotes: 51.38412 seconds
Time saved: 0.36035 seconds; 0.696%
The difference is small, but a saving nonetheless. I would always recommend using single quotes over double quotes.
str_replace('search', 'find', 'searched string');
…is faster than:
ereg_replace('search', 'find', 'searched string');
Obviously, it is necessary to use ereg_replace() (or preg_replace(), which is often a faster alternative) when using regular expressions. I done an experiment to see how much difference there is. I replaced a string 5,000,000 times with ereg_replace(), 5,000,000 times with preg_replace(), followed by 5,000,000 times with str_replace(). I replace the same string, not using regular expressions. Here are my results:
ereg_replace(): 26.65647 seconds
preg_replace(): 25.18324 seconds
str_replace(): 10.26872 seconds
Time saved (against ereg_replace()): 16.38775 seconds: 61.478%
Time saved (against preg_replace()): 14.91452 seconds: 59.224%
So, it's better to use str_replace() rather than ereg_replace() and preg_replace(), when you are not using regular expressions.
++$var;
…is faster than this:
$var++;
This rule also applies to decrementation as well. To test this assertion, I created two for loops. The first for loop used the post-increment option, while the second for loop used pre-increment instead. The total number of iterations was 10,000,000. Here are the results:
Post-increment: 2.148 seconds
Pre-increment: 1.692 seconds
Time saved: 0.456 seconds; 21.23%
if(ereg('[0123456789]', $number)) {
// Is integer
}else{
// Is not integer
}
It is much faster to do this instead:
if(ctype_digit($number)) {
// Is integer
}else{
// Is not integer
}
To test this, I used ereg('[0123456789]', $number) 1,000,000 times, followed by using ctype_digit($number) 1,000,000 times. Here are the results:
Regular Expressions: 2.401 seconds
ctype_digit: 0.985 seconds
Time saved: 1.416 seconds; 58.98%
split(): 5.453 seconds
explode(): 3.556 seconds
Time saved: 1.897 seconds; 34.79%
date('U'): 19.162 seconds
time(): 0.057 seconds
Time saved: 19.105 seconds; 99.7%
while(++$a<100000000){}
for(;++$a<100000000;){}
do{}while(++$a<100000000)
Here are my results:
while(++$a<100000000){}: 15.519 seconds
for(;++$a<100000000;){}: 17.577 seconds
do{}while(++$a<100000000): 13.744 seconds
As you can see, my results show that a do-while loop is 21.81% faster, compared to a for loop.