Home > Backend Development > PHP7 > Introducing the comparison between php7 and php5

Introducing the comparison between php7 and php5

coldplay.xixi
Release: 2023-02-17 18:58:01
forward
2011 people have browsed it

Introducing the comparison between php7 and php5

Recommendation (free): PHP7

When PHP7 came out, it was said to be much faster than the old version. Times, the speed and efficiency of various open source frameworks or systems running on PHP7 have increased several times. Anyway, both the media and developers are fanning the flames, no, they should be full of praise.
I will just watch you show off quietly and say nothing.

Generally, I am the last person to upgrade mobile phone systems because I don’t want to step into the trap. After all, systems like iOS and Android will have bugs, not to mention the most hacked languages ​​in the world.

The time has come today to see if PHP7 is as awesome as the legend says.

Install two PHP versions

http://php.net/ There is already the latest version of PHP7, you can download it yourself.
In order to test the performance of PHP5 and PHP7 (PHP6 has been abandoned, distressed 1s), I installed two php versions in different directories.

The installation process is skipped. Regardless of source code installation or package management tool installation, just remember your own path.

PHP7:

# /usr/local/php7/bin/php -vPHP 7.1.5 (cli) (built: May 13 2017 23:36:41) ( NTS )Copyright (c) 1997-2017 The PHP GroupZend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
Copy after login

PHP5:

# /usr/bin/php -vPHP 5.6.30 (cli) (built: Jan 19 2017 22:31:39)Copyright (c) 1997-2016 The PHP GroupZend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
Copy after login

Environment description: In order to ensure the best test effect, this test is conducted directly in the production environment, which is closer to the real situation.
Operating system: CentOS 7.2 64-bit
Basic configuration: 1 core 1GB 1Mbps
Host brand: Tencent Cloud

Competition between PHP7 and PHP5

1. Pure php script test

##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);}}}
Copy after login

PHP5 version test:

time /usr/bin/php test.php real    0m0.301suser    0m0.239ssys     0m0.050s--------------------------time /usr/bin/php test.phpreal    0m0.310suser    0m0.241ssys     0m0.054s--------------------------time /usr/bin/php test.phpreal    0m0.289suser    0m0.238ssys     0m0.050s
Copy after login

PHP7 version test :

time /usr/local/php7/bin/php test.phpreal    0m0.087suser    0m0.063ssys     0m0.024s-------------------------------------time /usr/local/php7/bin/php test.phpreal    0m0.106suser    0m0.073ssys     0m0.033s--------------------------------------time /usr/local/php7/bin/php test.phpreal    0m0.083suser    0m0.061ssys     0m0.022s
Copy after login

It can be seen from the data that the performance of php7 has been improved by 3 to 4 times through a simple PHP script test.

2.php database operation test

First we create a user table:

Table: test_userCreate Table: CREATE TABLE `test_user` (`uid` int(11) NOT NULL AUTO_INCREMENT,`name` char(100) NOT NULL DEFAULT &#39;&#39;,PRIMARY KEY (`uid`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
Copy after login

Insert a piece of data into the test_user table:

insert into test_user (uid,name) values (1,"dada");MariaDB [test]> select * from test_user;+-----+------+| uid | name |+-----+------+|   1 | dada |+-----+------+
Copy after login
Create the database test script test_db.php and ensure that both your PHP versions have the PDO extension installed.

/usr/bin/php -m|grep pdopdo_mysqlpdo_sqlite/usr/local/php7/bin/php -m|grep pdopdo_mysqlpdo_sqlite
Copy after login
My two PHP versions have PDO installed (do not use the php_mysql extension anymore, it is outdated, PHP7 has been completely abandoned, and mysqli is not recommended).

Next we write a script through PDO to test the performance comparison of select execution 500,000 times:

$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'];}}
Copy after login
PHP5 test test_db.php:

time /usr/bin/php test_db.phpreal    0m48.396suser    0m11.149ssys     0m3.998sreal    0m51.447suser    0m11.800ssys     0m4.395sreal    0m51.517suser    0m11.733ssys     0m4.439s
Copy after login
PHP7 test test_db.php:

real    0m47.900suser    0m9.875ssys     0m4.130sreal    0m46.977suser    0m9.760ssys     0m3.983sreal    0m50.010suser    0m10.268ssys     0m4.307s
Copy after login
This time the script executed 500,000 queries. The user execution time of the script executed by PHP7 is almost one second less than that of PHP5! It's one second less, not one millisecond.

3.PHP framework test

  • thinkphp

Domestic affirmation It is the first choice thinkphp framework, choose the latest thinkphp5. I downloaded the thinkphp5.0.9 version directly from the official website.

  • (1) Framework entry test

Test under PHP5:

time /usr/bin/php ./public/index.phpreal    0m0.036suser    0m0.026ssys     0m0.010sreal    0m0.038suser    0m0.026ssys     0m0.012sreal    0m0.041suser    0m0.032ssys     0m0.009s
Copy after login
Test under PHP7:

time /usr/local/php7/bin/php ./public/index.phpreal    0m0.027suser    0m0.021ssys     0m0.005sreal    0m0.027suser    0m0.018ssys     0m0.009sreal    0m0.025suser    0m0.023ssys     0m0.002s
Copy after login
In the entrance test, you can see that there is not much difference between PHP and PHP7, but PHP7 is still slightly faster.

  • (2) Framework logic test

    Reuse the logic of the first step at the framework entrance:

<?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);}}}}}
Copy after login
PHP5 version:

time /usr/bin/php ./public/index.phpreal    0m0.538suser    0m0.463ssys     0m0.072sreal    0m0.454suser    0m0.386ssys     0m0.065sreal    0m0.387suser    0m0.331ssys     0m0.055s
Copy after login
PHP7 version:

time /usr/local/php7/bin/php ./public/index.phpreal    0m0.150suser    0m0.123ssys     0m0.024sreal    0m0.137suser    0m0.105ssys     0m0.031sreal    0m0.123suser    0m0.096ssys     0m0.026s
Copy after login
Using the PHP7 version in the thinkphp framework, the performance improvement is about 4 times that of the PHP5 version!

  • laravel

Then we test the framework of the most popular PHP artist.

  • (1) Framework entrance test

    PHP5 version:

time /usr/bin/php ./public/index.phpreal    0m0.104suser    0m0.081ssys     0m0.022sreal    0m0.148suser    0m0.122ssys     0m0.025sreal    0m0.122suser    0m0.100ssys     0m0.021s
Copy after login
PHP version

time /usr/local/php7/bin/php ./public/index.phpreal    0m0.079suser    0m0.064ssys     0m0.015sreal    0m0.081suser    0m0.067ssys     0m0.014sreal    0m0.067suser    0m0.054ssys     0m0.013s
Copy after login
We can see In the laravel framework entry test, there is not much difference in performance between PHP5 and PHP7, but even the fastest version of PHP5, 0.081s, is slower than the slowest version of PHP7, 0.067s. So PHP7 is still better.

  • (2) Framework logic test

    Try to add a little logic, like thinkphp, to reuse the test logic.
    First modify the laravel routing and directly call the index method of UserController:

Route::get('/', 'UserController@index');
Copy after login
Write the test logic in the index method:

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);}}}}
Copy after login
PHP5 version

time /usr/bin/php ./public/index.phpreal    0m0.510suser    0m0.377ssys     0m0.079sreal    0m0.627suser    0m0.447ssys     0m0.091sreal    0m0.519suser    0m0.436ssys     0m0.079s
Copy after login
PHP7 version

time /usr/local/php7/bin/php ./public/index.phpreal    0m0.201suser    0m0.167ssys     0m0.032sreal    0m0.216suser    0m0.174ssys     0m0.040sreal    0m0.169suser    0m0.134ssys     0m0.034s
Copy after login
PHP7 performance has been improved by 3 to 4 times

Stress test

1000 requests, 50 concurrency

  • PHP5 version:

Three samples are as follows:

    ##PHP7 version:

Through the stress test, under the PHP7 version, you can see that the average of the single request time and request completion time, as well as the actual running time of each connection request, are less time-consuming. The most important thing is The performance index QPS is also higher than the PHP5 version.

Opcache performance test of PHP7

Enable opcache:


Damn it! PHP7 with opcache turned on is indeed about to take off. Compared with PHP7 that has not been turned on, the performance has improved by more than ten times, and compared with PHP5, it has improved by nearly 20 times! Asking if you are afraid!

PHP script test data is as follows:

PHP7 vs PHP5

Pure PHP script test process, PHP7 performance About 3 to 4 times that of PHP5.

The stress test data is as follows:

PHP5 vs PHP7 vs PHP7 OPCACHE

Conclusion:

This article makes a simple comparison between PHP7 and PHP5. The performance is indeed improved by 3 to 4 times. Whether in PHP pure scripts or in frameworks, PHP7's high-performance performance is consistent.

PHP7 is fast, PHP7 OpCache is fast, PHP7 is really awesome, a new era of PHP has arrived, use it quickly!

The above is the detailed content of Introducing the comparison between php7 and php5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template