Heim  >  Artikel  >  Backend-Entwicklung  >  PHP 7 新增内容介绍

PHP 7 新增内容介绍

WBOY
WBOYOriginal
2016-08-08 09:21:40827Durchsuche

PHP 7 新增内容介绍

2015年的夏天,虽然来得不算火热,但是在互联网技术的夏天,比任何一年都更为火热。
刚刚才结束了 5 月底的网易、支付宝、携程以及多家云存储厂商的接连故障的坏消息,6月上旬则迎来了编程语言界两大好消息,第一件是 Swift 2.0 发布以及开源,另一件是PHP 7 alpha版正式发布。这两件大事,都是可以载入相应的编程语言的史册级的事件。
Swift 2.0 的事,咱先不说了,本文的重点是带着大家见识一下鸟哥等众位大神打磨了2年的PHP 7,看看是否真的如之前所说的霸气。以及测试了一下现有软件和扩展的兼容性。
PHP7的安装,真是非常地向下兼容,下载,解压,把之前的配置命令用上,一路回车下去,毫无违和感。为了不影响现有的环境的运行,所有专门开辟了目录 。
配置参数如下:
--prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli --with-pdo-mysql --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts<br>安装好之后,做上软链接:<br><precourier new margin-top:0px margin-bottom:10px line-height:20px word-break:break-all word-wrap:break-word white-space:pre-wrap background-color:rgb>ln -s /usr/local/php7/bin/php /usr/bin/php7 ln -s /usr/local/php7/bin/php-config /usr/bin/php7-config ln -s /usr/local/php7/bin/phpize /usr/bin/php7ize ln -s /usr/local/php7/sbin/php-fpm /usr/sbin/php7-fpm<br>php7 -v 看到了我们熟悉的提示:<br><precourier new margin-top:0px margin-bottom:10px line-height:20px word-break:break-all word-wrap:break-word white-space:pre-wrap background-color:rgb>[root@localhost test]# php7 -v PHP 7.0.0alpha1 (cli) (built: Jun 13 2015 11:33:39) Copyright (c) 1997-2015 The PHP Group Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies<br>首先做的是性能评测,评测机型,首都在线云主机,4核 CPU Intel(R) Xeon(R) CPU E5-2680 0 @ 2.70GHz,内存4G,操作系统 Centos 6.5。<br>随便写了三段程序:<br>第一段,生成一个 60 万元素的数组,通过查找key 的方式,来确定key是否存在。<br><precourier new margin-top:0px margin-bottom:10px line-height:20px word-break:break-all word-wrap:break-word white-space:pre-wrap background-color:rgb><?php $a = array(); for($i=0;$i<600000;$i&#43;&#43;){ $a[$i] = $i; } foreach($a as $i) { array_key_exists($i, $a); }
首先是 PHP 5.3.17 版。
[root@localhost test]# time php search_by_key.php real 0m0.389s user 0m0.337s sys 0m0.051s [root@localhost test]# time php search_by_key.php real 0m0.378s user 0m0.308s sys 0m0.062s [root@localhost test]# time php search_by_key.php real 0m0.378s user 0m0.317s sys 0m0.061s<br>其次是 PHP7 版。<br><precourier new margin-top:0px margin-bottom:10px line-height:20px word-break:break-all word-wrap:break-word white-space:pre-wrap background-color:rgb>[root@localhost test]# time php7 search_by_key.php real 0m0.139s user 0m0.055s sys 0m0.048s [root@localhost test]# time php7 search_by_key.php real 0m0.089s user 0m0.058s sys 0m0.030s [root@localhost test]# time php7 search_by_key.php real 0m0.097s user 0m0.065s sys 0m0.022s<br>这刚出手,就名不虚传,响应时间在PHP7下运行变为原来的1/4。真牛!<br>那我还得搞俩试试,第二段,还是上面的这个方式,不过由于速度较慢,所以变成了一个60000个元素的数组,查找值。<br>代码如下:<br><precourier new margin-top:0px margin-bottom:10px line-height:20px word-break:break-all word-wrap:break-word white-space:pre-wrap background-color:rgb><?php $a = array(); for($i=0;$i<60000;$i&#43;&#43;){ $a[$i] = $i; } foreach($a as $i) { array_search($i, $a); } [root@localhost test]# time php search_by_val.php real 0m24.296s user 0m24.184s sys 0m0.025s [root@localhost test]# time php search_by_val.php real 0m25.523s user 0m25.317s sys 0m0.026s [root@localhost test]# time php search_by_val.php real 0m26.026s user 0m25.478s sys 0m0.092s
等待的时间,总是觉得很漫长,三次测试,花掉了75秒多。下面,PHP 7 登场了。
[root@localhost test]# time php7 search_by_val.php real 0m3.439s user 0m3.410ssys 0m0.008s [root@localhost test]# time php7 search_by_val.php real 0m3.426suser 0m3.409s sys 0m0.007s [root@localhost test]# time php7 search_by_val.php real 0m3.616suser 0m3.400s sys 0m0.018s
吊咋天,有没有!速度整整提高了将近7倍。
笔者激动的心情难以言表,顺手又整了一个比较高效的素数算法。算出2000000以内的素数的数目,
这次咱们 PHP7先开始。
[root@localhost test]# time php7 prime_v3.php 2000000 prime number count under 2000000 is :148933 real 0m1.211s user 0m1.191s sys 0m0.015s [root@localhost test]# time php7 prime_v3.php 2000000 prime number count under 2000000 is :148933 real 0m1.221s user 0m1.207s sys 0m0.010s [root@localhost test]# time php7 prime_v3.php 2000000 prime number count under 2000000 is :148933 real 0m1.220s user 0m1.201s sys 0m0.015s
速度稳定在 1.2 S
而 PHP 5.3 呢,这次比上一次的差距小点了,但是PHP7速度也是它的3倍到4倍之间。
[root@localhost test]# time php prime_v3.php 2000000 prime number count under 2000000 is :148933 real 0m4.425s user 0m4.380s sys 0m0.023s [root@localhost test]# time php prime_v3.php 2000000 prime number count under 2000000 is :148933 real 0m4.457s user 0m4.414s sys 0m0.032s [root@localhost test]# time php prime_v3.php 2000000 prime number count under 2000000 is :148933 real 0m4.464s user 0m4.399s sys 0m0.046s
至此,我们基本可以说明问题。这些代码,并没有使用复杂的函数库,也没有大量的网络和IO,性能却得到了至少3倍以上的优化。这真是一个历史性的进步。而在我们过去的性能评测中,语言层面的性能,往往是忽略的,为什么这么讲呢,比如在 XHProf中,就专门有一个选项,XHPROF_FLAGS_NO_BUILTINS,用于对内置的函数或者内部函数不做分析,比如数组、日期等的函数。因为大家往往错过了这块的提升空间,当然,一般人也无法在这块提升,所以才有了 HHVM,也激发了今天的PHP 7。
要想获得求素数的算法,请关注优才网公众账号,输入关键字“素数”获取哦。
一轮测试下来,激发起了笔者对PHP 7更多了解的兴趣,想看看扩展和一些常见的框架支持如何,顺手做了如下四个测试。
首先是XHProf,作为一名专注于性能优化的架构师,XHProf 真是看家神器之一,分分钟定位程序性能问题,PHP 7来了,XHProf不能丢啊,一看现在的版本,还是2013年的,只好从github上Download 了一份,结果phpize 还好好的,

configure还好好的

make 之后就悲剧了,看来是底层数据结构改了的原因,期待鸟哥升级啊。

既然 XHProf 用不了,云端的XHProf OneAPM能不能用呢?最近以来,OneAPM 用得越来越多了,不用自己埋代码,不用太多配置,商业的就是不一样。安装,结果,也是悲剧,得找他们老大反映反映去,如果搞不定,就把鸟哥聘请为PHP 技术顾问吧,保证你分分钟搞定。

对于扩展,我是没有信心再测下去了,下面测两个常用的东东,一个是Wordpress,虽然网站被和谐了,但是是什么地球人都知道吧。另外一个是ThinkPHP, 这是国内使用量最广泛的PHP开发框架,绝对第一,不是之一。笔者也是TP的粉丝。
是该夸这两个软件做得好呢?还是该夸 PHP 7的兼容做得好呢,我也不知道了,反正笔者看到了初步正常。

Wordpress 后台,在PHP 7 做为 FastCGI 后端时,运行正常。

ThinkPHP 最新版本3.2.3 在PHP 7 下运行正常。
好了,折腾了一个周末的下午 PHP 7,就成为了PHP7 的粉丝,也从中看到了PHP更加光明的未来,我大PHP将继续横扫中国互联网技术业界,无可匹敌,PHP技术人员将更为紧缺。也期待更多的PHP 技术人员,不仅仅是学一门语言,学一种语法,而站在更高的高度,做白盒运维,关注性能优化,成为全栈工程师,拿高薪、迎娶白富美,实现人生理想。 

参考来源: 
PHP 7 新增内容介绍
http://www.lai18.com/content/434544.html

以上就介绍了PHP 7 新增内容介绍,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:php遍历多维数组改变数组的值Nächster Artikel:PHP之冒泡排序