Comparison of Speeds in PHP
Author |
Message |
DtY
|
Posted: Tue Jun 23, 2009 4:28 pm Post subject: Comparison of Speeds in PHP |
|
|
I was wondering today just how much less efficient is double quotes than single quotes, and other such stuff. Here are the results:
$ php -v
PHP 5.2.9 (cli) (built: Apr 17 2009 03:29:12)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
Single Vs. Double Quotes
Single Quotes
php: | <?php
for ($i= 0; $i< 10000; $i++ ) {
echo 'Hello, World! ';
}
?> |
Double Quotes
php: | <?php
for ($i= 0; $i< 10000; $i++ ) {
echo "Hello, World! ";
}
?> |
Results
code: | Single Double
user 0m0.033s 0m0.022s
sys 0m0.022s 0m0.027s
total 0m0.055s 0m0.049s |
So, it seems that double quoted strings actually run faster (unexpected since double quoted strings support interpolation where single quoted strings don't). I am guessing this is a design flaw in PHP, and might be fixed in the future? Single quoted strings should be able to run faster than double quoted strings.
Interpolation Vs. Concatenation
Interpolation
php: | <?php
for ($i= 0; $i< 10000; $i++ ) {
echo "Hello, $i ";
}
?> |
Concatenation
php: | <?php
for ($i= 0; $i< 10000; $i++ ) {
echo "Hello, " . $i;
}
?> |
Results
code: | Interpolation Concatenation
user 0m0.065s 0m0.049s
sys 0m0.037s 0m0.045s
total 0m0.102s 0m0.094s |
No surprises here. Concatenation runs faster than interpolation, but not significantly
++ Vs. +=1
++
php: | <?php
for ($i=0; $i<10000; $i++);
?> |
+=1
php: | <?php
for ($i=0; $i<10000; $i+=1);
?> |
total
code: | ++ +=1
user 0m0.015s 0m0.024s
sys 0m0.014s 0m0.010s
total 0m0.029s 0m0.034s |
This is good, the nicer one runs faster.
(These are just on my computer, but the trends should be the same. As far as I know, they're accurate, unless PHP is doing optimizations on some of them, that it might not do in a place that's so obvious) |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rdrake
|
Posted: Tue Jun 23, 2009 5:33 pm Post subject: RE:Comparison of Speeds in PHP |
|
|
Try comparing i++ to ++i. PHP is full of all sorts of things like this. |
|
|
|
|
|
DtY
|
Posted: Tue Jun 23, 2009 5:40 pm Post subject: RE:Comparison of Speeds in PHP |
|
|
code: | $i++ ++$i
real 0m0.194s 0m0.015s
user 0m0.024s 0m0.014s
total 0m0.218s 0m0.029s |
Oh wow
I wonder how often anyone uses the return values from pre-/post-increments. Is there any use to having both of them? (Other than c does) |
|
|
|
|
|
DemonWasp
|
Posted: Tue Jun 23, 2009 7:40 pm Post subject: RE:Comparison of Speeds in PHP |
|
|
The ++i versus i++ thing is fairly frequent - even C++ has had issues with it. The biggest issue is when the increment is being done on an object with an overloaded ++ operator. Then, the ++i just does the increment. The i++ version makes a copy of the object, runs the increment on the copy it's going to keep, then uses the "backup" copy to complete the rest of the statement, even if there's nothing else to the statement.
For what it's worth, I've almost never used the return value from a ++i or i++ . You can get into trouble with that in some languages (cough C / C++). |
|
|
|
|
|
|
|