by Cory Rauch 2006-11-13 Category: Web-Performance
With the release of latest version of PHP, v5.2, one the biggest improvements where performance enhancements. So I decided to benchmark v5.2 vs v5.1 to see the impact of the new changes since I think many are looking forward to this release.
The benchmark was conducted on one computer where I first loaded PHP 5.1.6 then PHP 5.2.0, and only the PHP software changed. This benchmark was based on the PHP Benchmark suite available here:
http://phplens.com/benchmark_suite/Note: All results displayed in seconds, so basically you always want a smaller number.
Note: Both PHP versions where on Apache 2.0.53. Ubuntu's PHP 5.1.6 binary package was used for 5.1, and PHP 5.2.0 was compiled from source using default options (-02 Optimization).
The ResultsFirst I ran the Algorithms tests in the Benchmark Suite.
|
Encoding text data with base64 and utf8_encode |
|||
|
PHP 5.1.6: base64 |
0.1461 |
utf8_encode |
0.2423 |
|
PHP 5.2.0: base64 |
0.1865 |
utf8_encode |
0.2384 |
|
Encoding binary data with base64 and utf8_encode |
|||
|
PHP 5.1.6: base64 |
0.3142 |
utf8_encode |
1.2548 |
|
PHP 5.2.0: base64 |
0.3006 |
utf8_encode |
0.9067 |
|
Encoding binary data with base64 and rawurlencode |
|||
|
PHP 5.1.6: base64 |
0.3114 |
rawurlencode |
0.9455 |
|
PHP 5.2.0: base64 |
0.2913 |
rawurlencode |
0.7426 |
|
Comparing checksums with crc32 and md5 |
|||
|
PHP 5.1.6: crc32 |
0.0010 |
md5 |
0.0077 |
|
PHP 5.2.0: crc32 |
0.0009 |
md5 |
0.0066 |
|
Reading a binary file |
|||
|
PHP 5.1.6: fopen-fread-fclose x1.01 faster |
0.0622 |
file_get_contents |
0.0631 |
|
PHP 5.2.0: fopen-fread-fclose |
0.0638 |
file_get_contents |
0.0509 |
|
Testing parse_ini_file vs assoc arrays (see http://www.sitepoint.com/blog-post-view.php?id=154126) |
|||
|
PHP 5.1.6: parse_ini_file |
0.0580 |
assoc |
0.0762 |
|
PHP 5.2.0: parse_ini_file |
0.0607 |
assoc |
0.0705 |
As you can see from the results above, PHP 5.2 won 9 out of 12 tests. The biggest performance advantage came in the utf8_encode on binary data test, PHP 5.2 took about 30% less time to complete. Now we will go to the functions tests.
|
Strings: Call by ref vs call by value for 1 char string |
|||
|
PHP 5.1.6: Call by Ref |
0.0018 |
Call by Value |
0.0016 |
|
PHP 5.2.0: Call by Ref |
0.0017 |
Call by Value |
0.0016 |
|
Strings: Call by ref vs call by value for 10K string |
|||
|
PHP 5.1.6: Call by Ref |
0.0065 |
Call by Value |
0.0056 |
|
PHP 5.2.0: Call by Ref |
0.0084 |
Call by Value |
0.0055 |
|
Strings: Call by ref vs call by value for 100K string |
|||
|
PHP 5.1.6: Call by Ref |
0.0439 |
Call by Value |
0.0512 |
|
PHP 5.2.0: Call by Ref |
0.1730 |
Call by Value |
0.1637 |
|
Arrays: Call by ref vs call by value |
|||
|
PHP 5.1.6: Call by Ref |
0.0014 |
Call by Value |
0.0173 |
|
PHP 5.2.0: Call by Ref |
0.0016 |
Call by Value |
0.0160 |
|
Objects: Call by ref vs call by value |
|||
|
PHP 5.1.6: Call by Ref |
0.0039 |
Call by Value |
0.0064 |
|
PHP 5.2.0: Call by Ref |
0.0040 |
Call by Value |
0.0044 |
|
Objects: Return by ref vs by value |
|||
|
PHP 5.1.6: Return by Ref $x =& $f() |
0.0009 |
Return by Value |
0.0013 |
|
PHP 5.2.0: Return by Ref $x =& $f() |
0.0011 |
Return by Value |
0.0013 |
|
Objects: Return by partial ref vs by value |
|||
|
PHP 5.1.6: Return by Partial Ref $x = f() |
0.0012 |
Return by Value |
0.0013 |
|
PHP 5.2.0: Return by Partial Ref $x = f() |
0.0012 |
Return by Value |
0.0013 |
|
Strings: Calling function with short vs long name |
|||
|
PHP 5.1.6: 1 char function name |
0.0003 |
long 72 char function name |
0.0006 |
|
PHP 5.2.0: 1 char function name |
0.0004 |
long 72 char function name |
0.0008 |
One the functions test the story is a little different. Where PHP 5.2 won on 4 out of 16 test, tied with PHP 5.1.6 on 4 test, and PHP 5.1.6 won out on 8 tests. Particularly the “Call by ref vs call by value for 100K string” test was interesting since PHP 5.1.6 took half the amount of time. Next on to the regex tests.
|
Searching for substring with strpos vs perl-style regex |
|||
|
PHP 5.1.6: strpos |
0.0015 |
preg_match |
0.0032 |
|
PHP 5.2.0: strpos |
0.0015 |
preg_match |
0.0064 |
|
Searching for substring with perl-style regex vs ereg |
|||
|
PHP 5.1.6: preg_match |
0.0027 |
ereg |
0.0100 |
|
PHP 5.2.0: preg_match |
0.0024 |
ereg |
0.0069 |
Regular expression is a tad faster in PHP 5.2.0, winning out on 2 our 4 tests. With one tie and one loss left over. Next to the exception handling tests.
|
Testing PHP5 exceptions (with 1 throw) |
|||
|
PHP 5.1.6: No exceptions |
0.0006 |
With exceptions - throw 1 exception |
0.0236 |
|
PHP 5.2.0: No exceptions |
0.0012 |
With exceptions - throw 1 exception |
0.0208 |
|
Testing PHP5 exceptions (no throw) |
|||
|
PHP 5.1.6: No exception code |
0.0005 |
With exception code which is never invoked |
0.0010 |
|
PHP 5.2.0: No exception code |
0.0006 |
With exception code which is never invoked |
0.0010 |
PHP v5.2.0 wins on 1 test, and 1 tie. Though PHP 5.1.6 wins on two tests. Next the loops test.
|
50 ints array - Testing for loop with -- and ++ |
|||
|
PHP 5.1.6: for (--) |
0.0494 |
for(++) |
0.0475 |
|
PHP 5.2.0: for (--) |
0.0493 |
for(++) |
0.0470 |
|
50 ints array - Testing for loop with more complicated -- and ++ |
|||
|
PHP 5.1.6: for (--) |
0.0494 |
for(++) |
0.0494 |
|
PHP 5.2.0: for (--) |
0.0414 |
for(++) |
0.0552 |
|
50 ints array - foreach (no keys) versus for |
|||
|
PHP 5.1.6: Foreach |
0.0326 |
For |
0.0504 |
|
PHP 5.2.0: Foreach |
0.0248 |
For |
0.0422 |
|
50 ints array - foreach (no keys) versus reset and next |
|||
|
PHP 5.1.6: Foreach |
0.0308 |
Reset-Next |
0.0501 |
|
PHP 5.2.0: Foreach |
0.0327 |
Reset-Next |
0.0509 |
|
50 ints array - foreach (no keys) vs list(¸$v)=each |
|||
|
PHP 5.1.6: Foreach |
0.0258 |
List-Each |
0.1060 |
|
PHP 5.2.0: Foreach |
0.0250 |
List-Each |
0.1088 |
|
50 ints array - foreach (with keys) vs list($k¸$v)=each |
|||
|
PHP 5.1.6: Foreach |
0.0340 |
List-Each |
0.1175 |
|
PHP 5.2.0: Foreach |
0.0293 |
List-Each |
0.1204 |
|
Testing foreach versus array_map with php function with 50 int element array |
|||
|
PHP 5.1.6: Foreach |
0.0264 |
Array_Map |
0.0583 |
|
PHP 5.2.0: Foreach |
0.0241 |
Array_Map |
0.0707 |
|
50 strings array - Testing for loop with -- and ++ |
|||
|
PHP 5.1.6: for (--) |
0.0392 |
for(++) |
0.0444 |
|
PHP 5.2.0: for (--) |
0.0424 |
for(++) |
0.0572 |
|
50 ints array - Testing for loop with more complicated -- and ++ |
|||
|
PHP 5.1.6: for (--) |
0.0409 |
for(++) |
0.0485 |
|
PHP 5.2.0: for (--) |
0.0382 |
for(++) |
0.0409 |
|
50 strings array - foreach (no keys) versus for(++) |
|||
|
PHP 5.1.6: Foreach |
0.0266 |
For |
0.0452 |
|
PHP 5.2.0: Foreach |
0.0247 |
For |
0.0504 |
|
50 strings array - foreach (no keys) versus reset and next |
|||
|
PHP 5.1.6: Foreach |
0.0263 |
Reset-Next |
0.0652 |
|
PHP 5.2.0: Foreach |
0.0292 |
Reset-Next |
0.0585 |
|
50 strings array - foreach (no keys) vs list(¸$v)=each |
|||
|
PHP 5.1.6: Foreach |
0.0262 |
List-Each |
0.1103 |
|
PHP 5.2.0: Foreach |
0.0248 |
List-Each |
0.1102 |
|
50 strings array - foreach (with keys) vs list($k¸$v)=each |
|||
|
PHP 5.1.6: Foreach |
0.0322 |
List-Each |
0.1179 |
|
PHP 5.2.0: Foreach |
0.0318 |
List-Each |
0.1284 |
|
Testing foreach versus array_map with php function with 50 string element array |
|||
|
PHP 5.1.6: Foreach |
0.0544 |
Array_Map |
0.0281 |
|
PHP 5.2.0: Foreach |
0.0652 |
Array_Map |
0.0316 |
|
Testing foreach with crc32 versus array_map with php function with 50 element array |
|||
|
PHP 5.1.6: Foreach |
0.0815 |
Array_Map |
0.0676 |
|
PHP 5.2.0: Foreach |
0.0798 |
Array_Map |
0.0631 |
PHP 5.2.0 wins on 17 out 30 test. Next up the Variables test.
|
Reading CONSTANTS versus Global Variables |
|||
|
PHP 5.1.6: Constants |
0.0003 |
Globals |
0.0063 |
|
PHP 5.2.0: Constants |
0.0005 |
Globals |
0.0005 |
|
+ vs += integer operators |
|||
|
PHP 5.1.6: + integer |
0.0005 |
+= integer |
0.0003 |
|
PHP 5.2.0: + integer |
0.0007 |
+= integer |
0.0006 |
|
+ vs += string operators |
|||
|
PHP 5.1.6: + strings |
0.0018 |
+= strings |
0.0012 |
|
PHP 5.2.0: + strings |
0.0016 |
+= strings |
0.0047 |
|
+= vs + string operators |
|||
|
PHP 5.1.6: += strings |
0.0013 |
+ strings |
0.0012 |
|
PHP 5.2.0: += strings |
0.0017 |
+ strings |
0.0017 |
|
short vs long variable names |
|||
|
PHP 5.1.6: short var name (1 char) |
0.0012 |
long var name (72 chars) |
0.0007 |
|
PHP 5.2.0: short var name (1 char) |
0.0003 |
long var name (72 chars) |
0.0004 |
PHP 5.2.0 wins out on 3 out of 10 test. Particularly the reading of GLOBALS is faster. PHP 5.1.6 wins on 6 test though.
Other ImprovedSource Articles:
How to speed up the rendering of your website
Why we need a Javascript-Based Database?
Ubuntu Apache Performance Tip