推薦(免費):PHP7
PHP7來一發
PHP7正式發佈到現在已經一年半了,剛出道就號稱比舊版本快了幾倍,各種開源框架或系統運行在PHP7上速度效率提高了幾倍,反正不管是媒體還是開發者都在煽風點火,不,應該是讚不絕口。
我就靜靜看你們裝逼,不說話。
一般手機系統升級我是跑最後的,因為不願意踩坑,畢竟iOS和Android這種系統都會出現bug,何況世界上被人駭的最多的語言。
今日時機已到,看看PHP7有沒有傳說的那麼王炸。
安裝兩個PHP版本
http://php.net/ 已經有最新PHP7的最新版本,大家可自行下載。
為了測試PHP5和PHP7(PHP6已被放棄,心疼1s)的效能,我在不同的目錄安裝了兩個php版本。
安裝過程就略過了,無論原始碼安裝或套件管理工具安裝,記得自己的路徑即可。
PHP7:
# /usr/local/php7/bin/php -v PHP 7.1.5 (cli) (built: May 13 2017 23:36:41) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
PHP5:
# /usr/bin/php -v PHP 5.6.30 (cli) (built: Jan 19 2017 22:31:39) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
環境說明:為了確保最佳的測試效果,本次測試在直接在生產環境進行,更逼近真實情況。
作業系統:CentOS 7.2 64位元
基礎配置:1核心1GB 1Mbps
主機品牌:騰訊雲端
PHP7與PHP5的對決
#PHP7與PHP5的對決
#1. 純php腳本測試
vim test.php
$arr = array();for ($i = 0; $i < 500000; $i++) { $arr[$i] = $i; } $tmp = array(); foreach ($arr as $i) { if ($i % 2 == 0) { $is_exists = array_key_exists($i, $arr); if ($is_exists) { array_push($tmp, $i); } } }
time /usr/bin/php test.php real 0m0.301s user 0m0.239s sys 0m0.050s -------------------------- time /usr/bin/php test.php real 0m0.310s user 0m0.241s sys 0m0.054s -------------------------- time /usr/bin/php test.php real 0m0.289s user 0m0.238s sys 0m0.050s
time /usr/local/php7/bin/php test.php real 0m0.087s user 0m0.063s sys 0m0.024s ------------------------------------- time /usr/local/php7/bin/php test.php real 0m0.106s user 0m0.073s sys 0m0.033s -------------------------------------- time /usr/local/php7/bin/php test.php real 0m0.083s user 0m0.061s sys 0m0.022s
2.php資料庫操作測試
#首先我們建立一個使用者表:Table: test_user Create Table: CREATE TABLE `test_user` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `name` char(100) NOT NULL DEFAULT '', PRIMARY KEY (`uid`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
insert into test_user (uid,name) values (1,"dada"); MariaDB [test]> select * from test_user; +-----+------+| uid | name | +-----+------+ | 1 | dada | +-----+------+
/usr/bin/php -m|grep pdo pdo_mysql pdo_sqlite /usr/local/php7/bin/php -m|grep pdo pdo_mysql pdo_sqlite
$host = "yourHost"; $user = "yourUser"; $pass = "yourPass"; $db = "test"; $port = 3306; try { $dbh = new PDO("mysql:host=$host;dbname=$db", $user, $pass); echo "Connected<p>"; } catch (Exception $e) { echo "Unable to connect: " . $e->getMessage() ."<p>"; } $sql = "select SQL_NO_CACHE * from test_user;"; $tmp = array(); for ($i=1; $i<=500000; $i++) { $ret = $dbh->query($sql); foreach ($ret as $row) { $tmp['id'] = $row['id']; $tmp['name'] = $row['name']; } }
time /usr/bin/php test_db.php real 0m48.396s user 0m11.149s sys 0m3.998s real 0m51.447s user 0m11.800s sys 0m4.395s real 0m51.517s user 0m11.733s sys 0m4.439s
real 0m47.900suser 0m9.875s sys 0m4.130s real 0m46.977s user 0m9.760s sys 0m3.983s real 0m50.010s user 0m10.268s sys 0m4.307s
3.PHP框架測試
thinkphp
(一)框架入口測試
PHP5下的測試:time /usr/bin/php ./public/index.php real 0m0.036s user 0m0.026s sys 0m0.010s real 0m0.038s user 0m0.026s sys 0m0.012s real 0m0.041s user 0m0.032s sys 0m0.009s
time /usr/local/php7/bin/php ./public/index.php real 0m0.027s user 0m0.021s sys 0m0.005s real 0m0.027s user 0m0.018s sys 0m0.009s real 0m0.025s user 0m0.023s sys 0m0.002s
在框架入口中重複使用第一步的邏輯:
<?phpnamespace app\index\controller; class Index { public function index() { $arr = array(); for ($i = 0; $i < 500000; $i++) { $arr[$i] = $i; } $tmp = array(); foreach ($arr as $i) { if ($i % 2 == 0) { $is_exists = array_key_exists($i, $arr); if ($is_exists) { array_push($tmp, $i); } } } } }
time /usr/bin/php ./public/index.php real 0m0.538s user 0m0.463s sys 0m0.072s real 0m0.454s user 0m0.386s sys 0m0.065s real 0m0.387s user 0m0.331s sys 0m0.055s
time /usr/local/php7/bin/php ./public/index.php real 0m0.150s user 0m0.123s sys 0m0.024s real 0m0.137s user 0m0.105s sys 0m0.031s real 0m0.123s user 0m0.096s sys 0m0.026s
laravel
PHP5版本:
time /usr/bin/php ./public/index.php real 0m0.104s user 0m0.081s sys 0m0.022s real 0m0.148s user 0m0.122s sys 0m0.025s real 0m0.122s user 0m0.100s sys 0m0.021s
time /usr/local/php7/bin/php ./public/index.php real 0m0.079s user 0m0.064s sys 0m0.015s real 0m0.081s user 0m0.067s sys 0m0.014s real 0m0.067s user 0m0.054s sys 0m0.013s
(二)框架邏輯測試
試著增加一點邏輯,和thinkphp一樣,重複使用測試邏輯。先修改laravel路由,直接呼叫UserController的index方法:
Route::get('/', 'UserController@index');
public function index() { $arr = array(); for ($i = 0; $i < 500000; $i++) { $arr[$i] = $i; } $tmp = array(); foreach ($arr as $i) { if ($i % 2 == 0) { $is_exists = array_key_exists($i, $arr); if ($is_exists) { array_push($tmp, $i); } } } }
time /usr/bin/php ./public/index.php real 0m0.510s user 0m0.377s sys 0m0.079s real 0m0.627s user 0m0.447s sys 0m0.091s real 0m0.519s user 0m0.436s sys 0m0.079s
time /usr/local/php7/bin/php ./public/index.php real 0m0.201s user 0m0.167s sys 0m0.032s real 0m0.216s user 0m0.174s sys 0m0.040s real 0m0.169s user 0m0.134s sys 0m0.034s
以上是PHP7有那麼牛逼嗎(php7.1 和 php5.6 橫向對比)的詳細內容。更多資訊請關注PHP中文網其他相關文章!