php7和php5 性能測試對比,php7是一個值得你嘗試的版本!

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

目前php7.0.2已經發布了。php7下載:http://php.net/downloads.phpthinkphp

鳥哥博客裏說:『PHP7的性能已經和HHVM至關了』

通過這段時間的折騰,我給的結論是:php7是一個值得你嘗試的版本,性能提高卓越! 緩存

 

分兩種狀況:第一直接請求簡單頁面;第二命令行執行計算密集型代碼。 php7


 

第一種:直接請求簡單頁面的測試

測試環境:   thinkphp 3.2.3 + php7(開啓opcache)工具

      thinkphp 3.2.3 + php5.4(開啓opcache)性能

全默認歡迎頁面測試

fastcgi用php5.4 和php7 兩個解釋器執行spa

測試工具:ab.net

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倍!!

 

最後,總結一下:

  一、非計算密集型的代碼,在開啓緩存的狀況,php7性能提高至少1/4以上;

  二、計算密集型php7總體是php5.4的3倍

  三、php7值得嘗試,可是是否能大規模應用到線上環境,須要時間的考驗。

相關文章
相關標籤/搜索