


Let's talk about whether PHP7 function type restrictions have an impact on performance? (Test discussion)
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!
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 - To avoid incorrect calls, indicate the type, only Parameters of the same type can be passed, especially when multiple people are developing collaboratively.
- 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 ProProcessor name Quad-Core Intel Core i7Memory 8 GBTotal number of cores 4
##AB - 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
- -n Number of stress test requests
- (3) Implement
/***** 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; }
for a total of five stress tests. The configuration and results are displayed as follows (deleted uniformly: | grep 'Requests per second')
- (4) Results
/*****第一次*****/ // 类型限定接口 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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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.

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

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.

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