Table of Contents
Stress test
1000 requests, 50 concurrency
Opcache performance test of PHP7
Home Backend Development PHP Tutorial What are the differences between php7 and php5? Comparison between php5 and php7

What are the differences between php7 and php5? Comparison between php5 and php7

Aug 06, 2018 pm 03:21 PM

This article introduces to you what is the difference between php7 and php5? The comparison between php5 and php7 has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

PHP7来一发

It has been a year and a half since PHP7 was officially released. When it debuted, it was claimed to be several times faster than the old version. Various open source frameworks or The speed and efficiency of the system 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://www.php.cn/ The latest version of PHP7 is already available, 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 -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
Copy after login

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

Showdown 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.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
Copy after login

PHP7 version test:

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
Copy after login

It can be seen from the data that the pure PHP script test shows that the performance of PHP7 has been improved by 3 to 4 times.

2. PHP database operation test

First we create a user table:

Table: test_user
Create 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 pdo
pdo_mysql
pdo_sqlite

/usr/local/php7/bin/php -m|grep pdo
pdo_mysql
pdo_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[&#39;id&#39;]   = $row[&#39;id&#39;];
    $tmp[&#39;name&#39;] = $row[&#39;name&#39;];
  }
}
Copy after login

PHP5 test test_db.php:

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
Copy after login

PHP7 test test_db.php:

real    0m47.900s
user    0m9.875s
sys     0m4.130s

real    0m46.977s
user    0m9.760s
sys     0m3.983s

real    0m50.010s
user    0m10.268s
sys     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.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
Copy after login

Test under PHP7:

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
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:

<?php
namespace 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.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
Copy after login

PHP7 version:

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
Copy after login

When using the PHP7 version in the thinkphp framework, the performance improvement is approximately 4 times the PHP5 version!

  • laravel

Then we test the most popular PHP artist framework.

  • (1) Framework entry test
    PHP5 version:

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
Copy after login

PHP version

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
Copy after login

We can see that in the laravel framework entry test, the performance difference between PHP5 and PHP7 is not big, but even though PHP5 is the fastest The 0.081s is also slower than the slowest PHP7 version of 0.067s. So PHP7 is still better.

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

Route::get(&#39;/&#39;, &#39;UserController@index&#39;);
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.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
Copy after login

PHP7 version

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
Copy after login

PHP7 performance has been improved by 3 to 4 times

Stress test

1000 requests, 50 concurrency

  • PHP5 version:

Three times the sample is as follows:

PHP7 version:

Through the stress test, under the PHP7 version, you can see the single request time and request completion time and The average actual running time of each connection request takes less time, and the most important performance indicator 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 almost 20 times! Asking if you are afraid!

PHP script test data is as follows:

PHP7 vs PHP5.png

Pure PHP script test process, PHP7 performance is about PHP5 3 to 4 times.

The stress test data is as follows:

##PHP5 vs PHP7 vs PHP7 OPCACHE.png

Conclusion:

This article is A simple comparison between PHP7 and PHP5 shows that 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!

Recommended related articles:

Explanation of the principle of the time function strtotime() in PHP

Comparison of ts and nts in PHP

The above is the detailed content of What are the differences between php7 and php5? Comparison between php5 and php7. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles