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.
Function
Recommended learning: "PHP Video Tutorial"
function testInt(int $intNum){ var_dump($intNum); } testInt("123"); // int(123)
Running environment
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)
/***** 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; }
/*****第一次*****/ // 类型限定接口 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 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!