php7和php5 性能测试对比,php7是一个值得你尝试的版本!

时间:2021-09-12 19:39:17

PHP7最引人注意的就是zend引擎的更新:PHPNG(PHP next generation,下一代PHP)

目前php7.0.2已经发布了。php7下载:http://php.net/downloads.php

鸟哥博客里说:『PHP7的性能已经和HHVM相当了』

经过这段时间的折腾,我给的结论是:php7是一个值得你尝试的版本,性能提升卓越! 

 

分两种情况:第一直接请求简单页面;第二命令行执行计算密集型代码。 


 

第一种:直接请求简单页面的测试

测试环境:   thinkphp 3.2.3 + php7(开启opcache)

      thinkphp 3.2.3 + php5.4(开启opcache)

全默认欢迎页面

fastcgi用php5.4 和php7 两个解释器执行

测试工具:ab

php5.4执行:

命令:

ab -n 5000 -c 100 http://127.0.0.1:8601/Index

结果  

Concurrency Level:      100
Time taken for tests: 12.609 seconds
Complete requests: 5000
Failed requests: 223
(Connect: 0, Receive: 0, Length: 223, Exceptions: 0)
Total transferred: 95169771 bytes
HTML transferred: 93699771 bytes
Requests per second: 396.55 [#/sec] (mean)
Time per request: 252.172 [ms] (mean)
Time per request: 2.522 [ms] (mean, across all concurrent requests)
Transfer rate: 7371.08 [Kbytes/sec] received

 php7执行:

Concurrency Level:      100
Time taken for tests: 10.071 seconds
Complete requests: 5000
Failed requests: 108
(Connect: 0, Receive: 0, Length: 108, Exceptions: 0)
Total transferred: 95164890 bytes
HTML transferred: 93694890 bytes
Requests per second: 496.47 [#/sec] (mean)
Time per request: 201.422 [ms] (mean)
Time per request: 2.014 [ms] (mean, across all concurrent requests)
Transfer rate: 9227.84 [Kbytes/sec] received

 

结论:php7和php5.4在同时开启opcache情况下,不执行复杂计算,php7性能有25%的提升

第二种:执行计算密集型代码

用经典的斐波那契做计算,a.php代码如下:

a.php:

function fib($n) {
if ($n == 1 || $n == 2) {
return 1;
}
return fib($n - 1) + fib($n - 2);
}

echo fib(40);

 

测试结果:

php7和php5 性能测试对比,php7是一个值得你尝试的版本!

 

结论:通过计算密集型计算,可以看到php7执行用了10s,php5.4是31s,php7性能是php5.4的3倍!!

 

最后,总结一下:

  1、非计算密集型的代码,在开启缓存的情况,php7性能提升至少1/4以上;

  2、计算密集型php7整体是php5.4的3倍

  3、php7值得尝试,但是是否能大规模应用到线上环境,需要时间的考验。