PHP之怪现象[3]
这次要说的怪现象在于PHP的性能问题,且不说PHP的性能有多差,因为毕竟是一个脚本语言,不能奢望其速度多快,但PHP的性能的奇怪问题确实很特别:
- 版本的提升并未带来性能的提升——PHP5比PHP4慢
- 通过一些字节码优化器来大幅度提高性能
| PHP: version 4.3.10 vs PHP: version 5.0.5 PC: Celeron 2.0Mhz / 512Mb | ||
| PHP 4 | PHP 5 | |
Creating int array[100 000] | ||
| for vs while | ||
| code | [ms] | |
| for($i=0;$i<10000;$i++) | 0.063 | 0.087 |
| while($i<10000)> | 0.057 | 0.086 |
Reading int array[100 000] | ||
| while vs foreach vs or | ||
| foreach($arr1 as $vl) {..} | 0.085 | 0.171 |
| while(list(,$vl) = each($arr2)) {..} | 0.261 | 0.335 |
| for($i=0;$i<10000;$i++) | 0.123 | 0.163 |
Reading apache log (16Mb) | ||
| file vs file_get_content vs fread | ||
| $a = file("t1.log"); | 0.270 | 0.274 |
| $b = file_get_contents("t1.log"); | 0.120 | 0.121 |
| .. $c = fread($hdl, $size); .. | 0.133 | 0.141 |
Parse vars names [100 000] | ||
| "test$i" vs "test".$i vs ‘test’.$i | ||
| for(…) {$c = "test$i";} | 0.316 | 0.354 |
| for(…) {$c = "test".$i;} | 0.205 | 0.236 |
| for(…) {$c = ‘test’.$i;} | 0.205 | 0.236 |
Parse and find text from apache log (32Mb) | ||
| ereg vs preg_match | ||
| eregi("2005:03:04",$txt); | 3.504 | 3.521 |
| preg_match("/2005:03:04/im",$txt); | 0.108 | 0.735 |
Split text apache log 200Kb (contain 2000 "-") | ||
| split vs explode | ||
| split("-",$txt); | 1.561 | 1.573 |
| explode("-",$txt); | 0.193 | 0.197 |
Count array size. $arr = int[100 000] | ||
| count vs sizeof | ||
| for($i=0;$i<count($arr);$i++) | 0.198 | 0.276 |
| for($i=0;$i<sizeof($arr);$i++) | 0.198 | 0.268 |
Create object by Ref (loop 100 000) | ||
| with ref vs without ref | ||
| (…) {$j = & new TestClass();} | 0.460 | 1.180 |
| (…) {$j = new TestClass(); } | 0.470 | 1.291 |
Random number generator (loop 100 000) | ||
| srand vs mt_srand | ||
| (…) { srand(); } | 0.185 | 0.193 |
| (…) { mt_srand(); } | 0.929 | 0.989 |
Calculate hash (loop 100 000) | ||
| srand vs mt_srand | ||
| (…) { md5($i."byster.net".$i); } | 1.306 | 1.365 |
| (…) { sha1($i."byster.net".$i);} | 1.492 | 1.662 |

没有评论:
发表评论