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執行用了10s,php5.4是31s,php7性能是php5.4的3倍!!
最后,總結一下:
1、非計算密集型的代碼,在開啟緩存的情況,php7性能提升至少1/4以上;
2、計算密集型php7整體是php5.4的3倍
3、php7值得嘗試,但是是否能大規模應用到線上環境,需要時間的考驗。