Table of Contents
PHP7 function type limitation
(1) Introduction
(2) Stress test
for a total of five stress tests. The configuration and results are displayed as follows (deleted uniformly: | grep 'Requests per second')
The pressure test is not too rigorous, the results are for reference only
Home Backend Development PHP7 Let's talk about whether PHP7 function type restrictions have an impact on performance? (Test discussion)

Let's talk about whether PHP7 function type restrictions have an impact on performance? (Test discussion)

Feb 15, 2022 am 10:35 AM
php7 function type

PHP7Does function type limitation have an impact on performance? The following article will talk about the impact on performance of PHP7 function data type restriction settings. I hope it will be helpful to everyone!

Let's talk about whether PHP7 function type restrictions have an impact on performance? (Test discussion)

This article mainly uses a simple stress test to explore the impact of setting or not limiting the data type of PHP7 function on performance. In addition, I will share the two experiences I encountered in my work. Small problems and their solutions. If there are any mistakes, please correct them.

PHP7 function type limitation

(1) Introduction

  • Function parameter type limitation (including return value, member attribute) starts from PHP5 Supported, but not many types are supported. PHP7 has extended it: int/string/bool/object, etc.
  • Function

    • To avoid incorrect calls, indicate the type, only Parameters of the same type can be passed, especially when multiple people are developing collaboratively.

      Recommended learning: "PHP Video Tutorial"

    • If it is not possible to automatically convert the data type, as follows, of course, the premise is that the type to be converted can be converted normally
    • This article is about the impact of test type restrictions on performance
    function testInt(int $intNum){
      var_dump($intNum);
    }
    testInt("123"); // int(123)
    Copy after login
  • Note that if the parameters and return values ​​are inconsistent with the set type, an error will be reported. It is not 100% confirmed and needs to be done manually. Convert

(2) Stress test

  • Running environment

    • PHP 7.2.34
    • Laravel 5.8
    • AB 2.3
  • ##Single machine configuration

      Model name MacBook Pro
    • Processor name Quad-Core Intel Core i7
    • Memory 8 GB
    • Total number of cores 4
  • ##AB
  • Use AB (Apache Benchmark) for stress testing. Since it is not a formal stress test, we only care about the comprehensive indicators: Requests per second (average number of requests per second)
    • Main parameters
    • -n Number of stress test requests
      • -c Number of concurrency
      • -p Specify the file that needs to carry parameters when making a POST request
      • -r It does not exit when encountering an error response. The operating system has protection measures against high concurrency attacks (apr_socket_recv: Connection reset by peer)
    • ##Setting project Set up two POST interfaces. There is no business logic, middleware operations, etc., as follows
  • /***** 1 普通接口 *****/
    // CommonUserController
    public function createUser(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|string',
            'age'  => 'required|integer',
            'sex'  => ['required', Rule::in([1, 2])],
        ]);
        (new CommonUserModel())->createUser($request['age'], $request['name'], $request['sex'], $request['address'] ?? '');
        return response()->json(['status' => 200, 'msg' => 'ok']);
    }
    // CommonUserModel
    public function createUser($sex, $age, $name, $address)
    {
        if(empty($sex) || empty($age) || empty($name))  return false;
        // 省略DB操作
        return true;
    }
    
    /***** 2 类型限定接口 *****/
    // TypeUserController
    public function createUser(Request $request): JsonResponse
    {
        $this->validate($request, [
            'name' => 'required|string',
            'age'  => 'required|integer',
            'sex'  => ['required', Rule::in([1, 2])],
        ]);
        (new TypeUserModel())->createUser($request['age'], $request['name'], $request['sex'], $request['address'] ?? '');
        return response()->json(['status' => 200, 'msg' => 'ok']);
    }
    // TypeUserModel
    public function createUser(int $age, string $name, int $sex, string $address): bool
    {
        if(empty($sex) || empty($age) || empty($name)){
            return false;
        }
        // 省略DB操作
        return true;
    }
    Copy after login
  • (3) Implement

for a total of five stress tests. The configuration and results are displayed as follows (deleted uniformly: | grep 'Requests per second')

    /*****第一次*****/
    // 类型限定接口 rps=456.16
    ab -n 100  -c 10 -p '/tmp/ab_post_data.json' -T 'application:json'  http://www.laravel_type_test.com/api/type/create_user
    // 普通接口 rps=450.12
    ab -n 100  -c 10 -p '/tmp/ab_post_data.json' -T 'application:json'  http://www.laravel_type_test.com/api/common/create_user
    
    /*****第二次*****/
    // 类型限定接口 rps=506.74
    ab -n 1000  -c 100 -p '/tmp/ab_post_data.json' -T 'application:json'  http://www.laravel_type_test.com/api/type/create_user
    // 普通接口 rps=491.24
    ab -n 1000  -c 100 -p '/tmp/ab_post_data.json' -T 'application:json'  http://www.laravel_type_test.com/api/common/create_user
    
    /*****第三次*****/
    // 类型限定接口 rps=238.43 
    ab -n 5000  -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/type/create_user
    // 普通接口 rps=237.16
    ab -n 5000  -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/common/create_user
    
    /*****第四次*****/
    // 类型限定接口 rps=209.21
    ab -n 10000  -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/type/create_user
    // 普通接口 rps=198.01
    ab -n 10000  -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/common/create_user
    
    /*****第五次*****/
    // 类型限定接口 rps=191.17
    ab -n 100000  -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/type/create_user
    // 普通接口 rps=190.55
    ab -n 100000  -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/common/create_user
    Copy after login
  • (4) Results

The pressure test is not too rigorous, the results are for reference only

  • The performance improvement of type limitation is not as big as expected, it is very small, but this way of writing is still recommended

  • For more programming-related knowledge, please visit:
  • programming video
! !

The above is the detailed content of Let's talk about whether PHP7 function type restrictions have an impact on performance? (Test discussion). 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 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)

How to install mongo extension in php7.0 How to install mongo extension in php7.0 Nov 21, 2022 am 10:25 AM

How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? Apr 02, 2024 pm 07:39 PM

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

How to solve the problem when php7 detects that the tcp port is not working How to solve the problem when php7 detects that the tcp port is not working Mar 22, 2023 am 09:30 AM

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

PHP Server Environment FAQ Guide: Quickly Solve Common Problems PHP Server Environment FAQ Guide: Quickly Solve Common Problems Apr 09, 2024 pm 01:33 PM

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

How to install and deploy php7.0 How to install and deploy php7.0 Nov 30, 2022 am 09:56 AM

How to install and deploy php7.0: 1. Go to the PHP official website to download the installation version corresponding to the local system; 2. Extract the downloaded zip file to the specified directory; 3. Open the command line window and go to the "E:\php7" directory Just run the "php -v" command.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Performance comparison analysis of golang function types Performance comparison analysis of golang function types Apr 28, 2024 am 10:57 AM

In Go language, function types have a significant impact on performance. Performance comparison shows that ordinary functions are the best (147.08MOPS), followed by anonymous functions (158.01MOPS), and finally closures (10.02MOPS). These types have different advantages in different scenarios: anonymous functions are suitable for callbacks, closures are suitable for state management, and ordinary functions are suitable for performance optimization.

Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Apr 01, 2025 pm 03:06 PM

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

See all articles