Tips for faster PHP scripts

1 view
Skip to first unread message

Vasanth Govind

unread,
Sep 22, 2007, 2:55:57 AM9/22/07
to IntelliBitz Technologies, learne...@googlegroups.com
Tips for faster PHP scripts
LINK:http://bitfilm.net/bitfilm/?p=11
LINK:http://bitfilm.net/?p=17


August 24, 2007 @ 11:34 am · Filed under PHP

I have listed my top tips for writing faster (optimized) PHP code:

1. Multiple arguments with echo.
When you use echo, you probably use something like this:

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!
2. Reduce Function Calls
Now it may seem obvious, but many scripts have unnecessary calls
to functions. Look at this:

$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%

3. Do you really need to keep everything?
Some scripts assign values to variables when they don't need to.
Take a look at this code:

$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.
4. Using single quotes
When using strings in PHP, you have the choice of either double
quotes ( " ) or single quotes ( ' ). With double quotes, everything in
between is parsed (so you can use variables between them). With single
quotes, the string is not parsed. So, this code...

$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.
5. str_replace() vs. ereg_replace() and preg_replace()
If you do not need to use regular expressions when replacing
strings, you should use str_replace() instead of other functions. This
code...

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.


6. Pre-increment is faster than post-increment
Try to pre-increment, rather than post-increment, where
possible. It is faster because post-increment creates a temporary
variable while in the process of incrementing. So, this:

++$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%

7. Regular Expressions for input validation?
It is always a good idea to try and avoid regular expressions,
where possible and practical. There are functions in PHP which will do
exactly what some regular expressions do, but faster. Take this
example:

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%

8. split() or explode()
The split() function supports regular expressions, while
explode() does not. It is often faster to use explode() when you do
not need to use regular expressions.

I done yet another test. I used split() to split a string
without regular expression requirements, and then used explode() to
split the same string. I repeated this 1,000,000 times. My results
are:

split(): 5.453 seconds
explode(): 3.556 seconds
Time saved: 1.897 seconds; 34.79%

9. Use time() rather than date('U')
When you want to get the current Unix timestamp, it is faster to
use time() rather than date('U'). To test this, I used the time()
function 100,000 times, followed by date('U') 100,000 times. My
results are as follows:

date('U'): 19.162 seconds
time(): 0.057 seconds
Time saved: 19.105 seconds; 99.7%

10. Fastest type of loop
In PHP, there are a number of loops available for you to use.
There are while loops, do-while loops, and for loops. To see which one
of these were fastest, I used each of them to perform 100,000,000
iterations. These are the loops which I used:

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.

Reply all
Reply to author
Forward
0 new messages