手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

PHP 性能分析与实验:性能的微观分析

[日期:2015-09-13] 来源:news.oneapm.com  作者:王鹏 [字体:大 中 小]

2.5、正则表达式和普通字符串操作

在字符串操作中,有一条常见的规则,即是能使用普通字符串操作方法替代的,就不要使用正则表达式来处理,用 C 语言操作 PCRE 做过正则表达式处理的童鞋应该清楚,需要先 compile,再 exec,也就是说是一个相对复杂的过程。现在就比较一下两者的差别。

对于简单的分隔,我们可以使用 explode 来实现,也可以使用正则表达式,比如下面的例子:

  1. ini_set("precision",16);
  2. function microtime_ex()
  3. {
  4. list($usec, $sec)= explode(" ", microtime());
  5. return $sec+$usec;
  6. }
  7. for($i=0; $i<1000000; $i++){
  8. microtime_ex();
  9. }

耗时在0.93-1S之间。

  1. [root@localhostphpperf]#time php7 pregstring.php
  2. real 0m0.941s 
  3. user 0m0.931s 
  4. sys 0m0.007s 
  5. [root@localhostphpperf]#time php7 pregstring.php
  6. real 0m0.986s 
  7. user 0m0.980s 
  8. sys 0m0.004s 
  9. [root@localhostphpperf]#time php7 pregstring.php
  10. real 0m1.004s 
  11. user 0m0.998s 
  12. sys 0m0.003s

我们再将分隔语句替换成:

  1. list($usec, $sec)= preg_split("#\s#", microtime());

得到如下数据,慢了近10-20%。

  1. [root@localhostphpperf]#time php7 pregstring1.php
  2. real 0m1.195s 
  3. user 0m1.182s 
  4. sys 0m0.004s 
  5. [root@localhostphpperf]#time php7 pregstring1.php
  6. real 0m1.222s 
  7. user 0m1.217s 
  8. sys 0m0.003s 
  9. [root@localhostphpperf]#time php7 pregstring1.php
  10. real 0m1.101s 
  11. user 0m1.091s 
  12. sys 0m0.005s

再将语句替换成:

  1. list($usec, $sec)= preg_split("#\s+#", microtime());

即匹配一到多个空格,并没有太多的影响。除了分隔外,查找我们也来看一个例子。

第一段代码:

  1. $str="China has a Large population";
  2. for($i=0; $i<1000000; $i++){
  3. if(preg_match("#l#i", $str))
  4. {
  5. }
  6. }

第二段代码:

  1. $str="China has a large population";
  2. for($i=0; $i<1000000; $i++){
  3. if(stripos($str,"l")!==false)
  4. {
  5. }
  6. }

这两段代码达到的效果相同,都是查找字符串中有无 l 或者 L 字符。

在 PHP 7 下运行效果如下:

  1. [root@localhostphpperf]#time php7 pregstring2.php
  2. real 0m0.172s 
  3. user 0m0.167s 
  4. sys 0m0.003s 
  5. [root@localhostphpperf]#time php7 pregstring2.php
  6. real 0m0.199s 
  7. user 0m0.196s 
  8. sys 0m0.002s 
  9. [root@localhostphpperf]#time php7 pregstring3.php
  10. real 0m0.185s 
  11. user 0m0.182s 
  12. sys 0m0.003s 
  13. [root@localhostphpperf]#time php7 pregstring3.php
  14. real 0m0.184s 
  15. user 0m0.181s 
  16. sys 0m0.003s

两者区别不大。再看看在 PHP5.6 中的表现。

  1. [root@localhostphpperf]#time php56 pregstring2.php
  2. real 0m0.470s 
  3. user 0m0.456s 
  4. sys 0m0.004s 
  5. [root@localhostphpperf]#time php56 pregstring2.php
  6. real 0m0.506s 
  7. user 0m0.500s 
  8. sys 0m0.005s 
  9. [root@localhostphpperf]#time php56 pregstring3.php
  10. real 0m0.348s 
  11. user 0m0.342s 
  12. sys 0m0.004s 
  13. [root@localhostphpperf]#time php56 pregstring3.php
  14. real 0m0.376s 
  15. user 0m0.364s 
  16. sys 0m0.003s

可见在 PHP 5.6 中表现还是非常明显的,使用正则表达式慢了20%。PHP7 难道是对已使用过的正则表达式做了缓存?我们调整一下代码如下:

  1. $str="China has a Large population";
  2. for($i=0; $i<1000000; $i++){
  3. $pattern ="#".chr(ord('a')+$i%26)."#i";
  4. if($ret = preg_match($pattern, $str)!==false)
  5. {
  6. }
  7. }

这是一个动态编译的 pattern。

  1. $str="China has a large population";
  2. for($i=0; $i<1000000; $i++){
  3. $pattern ="".chr(ord('a')+$i%26)."";
  4. if($ret = stripos($str, $pattern)!==false)
  5. {
  6. }
  7. }

在 PHP7 中,得到了如下结果:

  1. [root@localhostphpperf]#time php7 pregstring2.php
  2. real 0m0.351s 
  3. user 0m0.346s 
  4. sys 0m0.004s 
  5. [root@localhostphpperf]#time php7 pregstring2.php
  6. real 0m0.359s 
  7. user 0m0.352s 
  8. sys 0m0.004s 
  9. [root@localhostphpperf]#time php7 pregstring3.php
  10. real 0m0.375s 
  11. user 0m0.369s 
  12. sys 0m0.003s 
  13. [root@localhostphpperf]#time php7 pregstring3.php
  14. real 0m0.370s 
  15. user 0m0.365s 
  16. sys 0m0.005s

可见两者并不明显。而在 PHP 5.6 中,同样的代码:

  1. [root@localhostphpperf]#time php56 pregstring2.php
  2. real 0m1.022s 
  3. user 0m1.015s 
  4. sys 0m0.005s 
  5. [root@localhostphpperf]#time php56 pregstring2.php
  6. real 0m1.049s 
  7. user 0m1.041s 
  8. sys 0m0.005s 
  9. [root@localhostphpperf]#time php56 pregstring3.php
  10. real 0m0.923s 
  11. user 0m0.821s 
  12. sys 0m0.002s 
  13. [root@localhostphpperf]#time php56 pregstring3.php
  14. real 0m0.838s 
  15. user 0m0.831s 
  16. sys 0m0.004s

在 PHP 5.6 中,stripos 版明显要快于正则表达式版,由上两例可见,PHP7对正则表达式的优化还是相当惊人的。其次也建议,能用普通字符串操作的地方,可以避免使用正则表达式。因为在其他版本中,这个规则还是适用的。某 zend 大牛官方的分享给出如下数据:

  • stripos(‘http://’, $website) 速度是preg_match(‘/http:\/\//i’, $website) 的两倍
  • ctype_alnum()速度是preg_match(‘/^\s*$/’)的5倍;
  • “if ($test == (int)$test)” 比 preg_match(‘/^\d*$/’)快5倍

可以相见,正则表达式是相对低效的。

 

2.6、数组元素定位查找

在数组元素的查找中,有一个关键的注意点就是数组值和键的查找速度,差异非常大。了解过 PHP 扩展开发的朋友,应该清楚,数组在底层其实是 Hash 表。所以键是以快速定位的,而值却未必。下面来看例子。

首先们构造一个数组:

  1. $a=array();
  2. for($i=0;$i<100000;$i++){
  3. $a[$i]= $i;
  4. }

在这个数组中,我们测试查找值和查找键的效率差别。

第一种方法用 array_ search,第二种用 array_ key_ exists,第三种用 isset 语法结构。 代码分别如下:

  1. //查找值
  2. foreach($a as $i)
  3. {
  4. array_search($i, $a);
  5. }
  6. //查找键
  7. foreach($a as $i)
  8. {
  9. array_key_exists($i, $a);
  10. }
  11. //判定键是否存在
  12. foreach($a as $i)
  13. {
  14. if(isset($a[$i]));
  15. }

运行结果如下:

  1. [root@localhostphpperf]#time php7 array.php
  2. real 0m9.026s 
  3. user 0m8.965s 
  4. sys 0m0.007s 
  5. [root@localhostphpperf]#time php7 array.php
  6. real 0m9.063s 
  7. user 0m8.965s 
  8. sys 0m0.005s 
  9. [root@localhostphpperf]#time php7 array1.php
  10. real 0m0.018s 
  11. user 0m0.016s 
  12. sys 0m0.001s 
  13. [root@localhostphpperf]#time php7 array1.php
  14. real 0m0.021s 
  15. user 0m0.015s 
  16. sys 0m0.004s 
  17. [root@localhostphpperf]#time php7 array2.php
  18. real 0m0.020s 
  19. user 0m0.014s 
  20. sys 0m0.006s 
  21. [root@localhostphpperf]#time php7 array2.php
  22. real 0m0.016s 
  23. user 0m0.009s 
  24. sys 0m0.006s

由上例子可见,键值查找的速度比值查找的速度有百倍以上的效率差别。因而如果能用键值定位的地方,尽量用键值定位,而不是值查找。

 

2.7、对象与数组

在 PHP 中,数组就是字典,字典可以存储属性和属性值,而且无论是键还是值,都不要求数据类型统一,所以对象数据存储,既能用对象数据结构的属性存储数据,也能使用数组的元素存储数据。那么两者有何差别呢?

使用对象:

  1. clasSUSEr
  2. {
  3. public $uid;
  4. public $username;
  5. public $age;
  6. function getUserInfo()
  7. {
  8. return"UID:".$this->uid." UserName:".$this->username." Age:".$this->age;
  9. }
  10. }
  11. for($i=0; $i<1000000;$i++){
  12. $user =newUser();
  13. $user->uid= $i;
  14. $user->age = $i%100;
  15. $user->username="User".$i;
  16. $user->getUserInfo();
  17. }

使用数组:

  1. functiongetUserInfo($user)
  2. {
  3. return"UID:".$user['uid']." UserName:".$user['username']." Age:".$user['age'];
  4. }
  5. for($i=0; $i<1000000;$i++){
  6. $user =array("uid"=>$i,"age"=>$i%100,"username"=>"User".$i);
  7. getUserInfo($user);
  8. }

我们分别在 PHP5.3、PHP 5.6 和 PHP 7 中运行这两段代码。

  1. [root@localhostphpperf]#time phpobject.php
  2. real 0m2.144s 
  3. user 0m2.119s 
  4. sys 0m0.009s 
  5. [root@localhostphpperf]#time phpobject.php
  6. real 0m2.106s 
  7. user 0m2.089s 
  8. sys 0m0.013s 
  9. [root@localhostphpperf]#time php object1.php
  10. real 0m1.421s 
  11. user 0m1.402s 
  12. sys 0m0.016s 
  13. [root@localhostphpperf]#time php object1.php
  14. real 0m1.431s 
  15. user 0m1.410s 
  16. sys 0m0.012s

在 PHP 5.3 中,数组版比对象版快了近30%。

  1. [root@localhostphpperf]#time php56 object.php
  2. real 0m1.323s 
  3. user 0m1.319s 
  4. sys 0m0.002s 
  5. [root@localhostphpperf]#time php56 object.php
  6. real 0m1.414s 
  7. user 0m1.400s 
  8. sys 0m0.006s 
  9. [root@localhostphpperf]#time php56 object1.php
  10. real 0m1.356s 
  11. user 0m1.352s 
  12. sys 0m0.002s 
  13. [root@localhostphpperf]#time php56 object1.php
  14. real 0m1.364s 
  15. user 0m1.349s 
  16. sys 0m0.006s 
  17. [root@localhostphpperf]#time php7 object.php
  18. real 0m0.642s 
  19. user 0m0.638s 
  20. sys 0m0.003s 
  21. [root@localhostphpperf]#time php7 object.php
  22. real 0m0.606s 
  23. user 0m0.602s 
  24. sys 0m0.003s 
  25. [root@localhostphpperf]#time php7 object1.php
  26. real 0m0.615s 
  27. user 0m0.613s 
  28. sys 0m0.000s 
  29. [root@localhostphpperf]#time php7 object1.php
  30. real 0m0.615s 
  31. user 0m0.611s 
  32. sys 0m0.003s

到了 PHP 5.6 和 PHP7 中,两个版本基本没有差别,而在 PHP7 中的速度是 PHP5.6 中的2倍。在新的版本中,差别已几乎没有,那么为了清楚起见我们当然应该声明类,实例化类来存储对象数据。

 

2.8、getter 和 setter

从 Java 转过来学习 PHP 的朋友,在对象声明时,可能习惯使用 getter 和 setter,那么,在 PHP 中,使用 getter 和 setter 是否会带来性能上的损失呢?同样,先上例子。

无 setter版:

  1. classUser
  2. {
  3. public $uid;
  4. public $username;
  5. public $age;
  6. function getUserInfo()
  7. {
  8. return"UID:".$this->uid." UserName:".$this->username." Age:".$this->age;
  9. }
  10. }
  11. for($i=0; $i<1000000;$i++){
  12. $user =newUser();
  13. $user->uid= $i;
  14. $user->age = $i%100;
  15. $user->username="User".$i;
  16. $user->getUserInfo();
  17. }

有 setter版:

  1. classUser
  2. {
  3. public $uid;
  4. private $username;
  5. public $age;
  6. function setUserName($name)
  7. {
  8. $this->username = $name;
  9. }
  10. function getUserInfo()
  11. {
  12. return"UID:".$this->uid." UserName:".$this->username." Age:".$this->age;
  13. }
  14. }
  15. for($i=0; $i<1000000;$i++){
  16. $user =newUser();
  17. $user->uid= $i;
  18. $user->age = $i%100;
  19. $user->setUserName("User".$i);
  20. $user->getUserInfo();
  21. }

这里只增加了一个 setter。运行结果如下:

  1. [root@localhostphpperf]#time php7 object.php
  2. real 0m0.607s 
  3. user 0m0.602s 
  4. sys 0m0.004s 
  5. [root@localhostphpperf]#time php7 object.php
  6. real 0m0.598s 
  7. user 0m0.596s 
  8. sys 0m0.000s 
  9. [root@localhostphpperf]#time php7 object2.php
  10. real 0m0.673s 
  11. user 0m0.669s 
  12. sys 0m0.003s 
  13. [root@localhostphpperf]#time php7 object2.php
  14. real 0m0.668s 
  15. user 0m0.664s 
  16. sys 0m0.004s

从上面可以看到,增加了一个 setter,带来了近10%的效率损失。可见这个性能损失是相当大的,在 PHP 中,我们没有必要再来做 setter 和 getter了。需要引用的属性,直接使用即可。

 

2.9、类属性该声明还是不声明

PHP 本身支持属性可以在使用时增加,也就是不声明属性,可以在运行时添加属性。那么问题来了,事先声明属性与事后增加属性,是否会有性能上的差别。这里也举一个例子探讨一下。

事先声明了属性的代码就是2.8节中,无 setter 的代码,不再重复。而无属性声明的代码如下:

  1. classUser
  2. {
  3. function getUserInfo()
  4. {
  5. return"UID:".$this->uid." UserName:".$this->username." Age:".$this->age;
  6. }
  7. }
  8. for($i=0; $i<1000000;$i++){
  9. $user =newUser();
  10. $user->uid= $i;
  11. $user->age = $i%100;
  12. $user->username="User".$i;
  13. $user->getUserInfo();
  14. }

两段代码,运行结果如下:

  1. [root@localhostphpperf]#time php7 object.php
  2. real 0m0.608s 
  3. user 0m0.604s 
  4. sys 0m0.003s 
  5. [root@localhostphpperf]#time php7 object.php
  6. real 0m0.615s 
  7. user 0m0.605s 
  8. sys 0m0.003s 
  9. [root@localhostphpperf]#time php7 object3.php
  10. real 0m0.733s 
  11. user 0m0.728s 
  12. sys 0m0.004s 
  13. [root@localhostphpperf]#time php7 object3.php
  14. real 0m0.727s 
  15. user 0m0.720s 
  16. sys 0m0.004s

从上面的运行可以看到,无属性声明的代码慢了20%。可以推断出来的就是对于对象的属性,如果事先知道的话,我们还是事先声明的好,这一方面是效率问题,另一方面,也有助于提高代码的可读性呢。

 

2.10、图片操作 API 的效率差别

在图片处理操作中,一个非常常见的操作是将图片缩放成小图。缩放成小图的办法有多种,有使用 API 的,有使用命令行的。在 PHP 中,有 imagick 和 gmagick 两个扩展可供操作,而命令行则一般使用 convert 命令来处理。我们这里来讨论使用 imagick 扩展中的 API 处理图片的效率差别。

先上代码:

  1. function imagick_resize($filename, $outname)
  2. {
  3. $thumbnail =newImagick($filename);
  4. $thumbnail->resizeImage(200,200, imagick::FILTER_LANCZOS,1);
  5. $thumbnail->writeImage($outname);
  6. unset($thumbnail);
  7. }
  8. function imagick_scale($filename, $outname)
  9. {
  10. $thumbnail =newImagick($filename);
  11. $thumbnail->scaleImage(200,200);
  12. $thumbnail->writeImage($outname);
  13. unset($thumbnail);
  14. }
  15. function convert($func)
  16. {
  17. $cmd="find /var/data/ppt |grep jpg";
  18. $start = microtime(true);
  19. exec($cmd, $files);
  20. $index =0;
  21. foreach($files as $key =>$filename)
  22. {
  23. $outname=" /tmp/$func"."_"."$key.jpg";
  24. $func($filename, $outname);
  25. $index++;
  26. }
  27. $end = microtime(true);
  28. echo"$func $index files: ".($end- $start)."s\n";
  29. }
  30. convert("imagick_resize");
  31. convert("imagick_scale");

在上面的代码中,我们分别使用了 resizeImage 和 scaleImage 来进行图片的压缩,压缩的是常见的 1-3M 之间的数码相机图片,得到如下运行结果:

  1. [root@localhostphpperf]# php55 imagick.php
  2. imagick_ resize 169 files:5.0612308979034s 
  3. imagick_ scale 169 files:3.1105840206146s
  4. [root@localhostphpperf]# php55 imagick.php
  5. imagick_ resize 169 files:4.4953861236572s 
  6. imagick_ scale 169 files:3.1514940261841s
  7. [root@localhostphpperf]# php55 imagick.php
  8. imagick_ resize 169 files:4.5400381088257s 
  9. imagick_ scale 169 files:3.2625908851624s

169张图片压缩,使用 resizeImage 压缩,速度在4.5S以上,而使用 scaleImage 则在 3.2S 左右,快了将近50%,压缩的效果,用肉眼看不出明显区别。当然 resizeImage 的控制能力更强,不过对于批量处理而言,使用 scaleImage 是更好的选择,尤其对头像压缩这种频繁大量的操作。本节只是例举了图片压缩 API 作为例子,也正像 explode 和 preg_ split 一样,在 PHP 中,完成同样一件事情,往往有多种手法。建议采用效率高的做法。

以上就是关于 PHP 开发的10个方面的对比,这些点涉及到 PHP 语法、写法以及 API 的使用。有些策略随着 PHP 的发展,有的已经不再适用,有些策略则会一直有用。

有童鞋也许会说,在现实的开发应用中,上面的某些观点和解决策略,有点「然并卵」。为什么这么说呢?因为在一个程序的性能瓶颈中,最为核心的瓶颈,往往并不在 PHP 语言本身。即使是跟 PHP 代码中暴露出来的性能瓶颈,也常在外部资源和程序的不良写法导致的瓶颈上。于是为了做好性能分析,我们需要向 PHP 的上下游戏延伸,比如延伸到后端的服务上去,比如延伸到前端的优化规则。在这两块,都有了相当多的积累和分析,雅虎也据此提出了多达35条前端优化规则,这些同 PHP 本身的性能分析构成了一个整体,就是降低用户的访问延时。

所以前面两部分所述的性能分析,只是有助于大家了解 PHP 开发本身,写出更好的 PHP 程序,为你成为一个资深的 PHP 程序员打下基础,对于实际生产中程序的效率提升,往往帮助也不是特别显著,因为大家也看到,在文章的实例中,很多操作往往是百万次才能看出明显的性能差别。在现实的页面中,每一个请求很快执行完成,对这些基础代码的调用,往往不会有这么多次调用。不过了解这些,总是好的。

那么,对于一个程序而言,其他的性能瓶颈可能存在哪里?我们将深入探讨。所以在本系列的下两篇,我们将探讨 PHP 程序的外围效源的效率问题和前端效率问题,敬请期待。

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-09/122984.htm

linux
相关资讯       PHP  PHP性能分析 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款