Get the App
SLTechnology News&Howtos  ›  Development  › 

Does PHP7 function type qualification have any effect on performance?

Shulou Source: shulou.com Published: 2022-06-01 17:46:59 10月03日 Update

This article mainly introduces whether the PHP7 function type limitation has any impact on the performance, the article introduces in great detail, has a certain reference value, interested friends must read it!

Introduction to PHP7 function Type qualification (1)

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

Action

Function testInt (int $intNum) {var_dump ($intNum);} testInt ("123"); / / int (123)

Avoid error calls, indicate the type, and only pass parameters of the same type, especially when many people work together.

Recommended study: "PHP Video tutorial"

If the data type can not be converted automatically, as follows, of course, if the type to be converted can be converted normally.

This paper is just the degree of the influence of test type limitation on performance.

Note that if the parameters and return values are inconsistent with the set type, an error will be reported. If it is not 100% confirmed, it needs to be converted manually.

(2) pressure test

Operation environment

PHP 7.2.34

Laravel 5.8

AB 2.3

Stand-alone configuration

Model name MacBook Pro

Processor name Quad-Core Intel Core i7

Memory 8 GB

Total number of nuclei 4

AB

Number of requests for pressure test

-c concurrency

-p POST request specifies the file with the required parameters

-r does not quit in case of error response, and the operating system has protective measures against high concurrency attacks (apr_socket_recv: Connection reset by peer)

AB (Apache Benchmark) is used for pressure test. Since it is not a formal pressure test, only comprehensive indicators are concerned here: Requests per second (average requests per second)

Main parameters

Set up the project to set up two POST interfaces with no business logic, middleware operation, etc., as follows

/ * 1 ordinary interface * / CommonUserControllerpublic 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']);} / / CommonUserModelpublic function createUser ($sex, $age, $name, $address) {if (empty ($sex) | empty ($age) | empty ($name) return false; / omit DB operation return true } / * 2 TypeUserControllerpublic 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']);} / / TypeUserModelpublic function createUser (int $age, string $name, int $sex, string $address): bool {if (empty ($sex) | empty ($age) | empty ($name) {return false } / / omit DB operation return true;} (3) implementation

A total of five pressure tests were performed, and the configuration and results are shown as follows (unified deletion: | grep 'Requests per second')

/ * for the first time * / / Type qualified interface rps=456.16ab-n 100-c 10-p'/ tmp/ab_post_data.json'-T 'application:json' http://www.laravel_type_test.com/api/type/create_user// ordinary interface rps=450.12ab-n 100-c 10-p' / tmp/ab_post_data.json'-T 'application:json' http: / / www.laravel_type_test.com/api/common/create_user/* second * / / Type qualified interface rps=506.74ab-n 1000-c 100-p'/ tmp/ab_post_data.json'-T 'application:json' http://www.laravel_type_test.com/api/type/create_user// ordinary interface rps=491.24ab-n 1000-c 100-p' / tmp/ Ab_post_data.json'-T 'application:json' http://www.laravel_type_test.com/api/common/create_user/***** the third * / / Type-qualified interface 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/ / ordinary interface rps=237.16ab-n 5000-c 150-p'/ tmp/ab_post_data.json'-T 'application:json'-r http://www.laravel_type_test.com/api/common/create_user/***** the fourth * / / type qualified interface rps=209.21ab-n 10000-c 150-p' / tmp/ab_post_data.json'-T 'application:json'- R http://www.laravel_type_test.com/api/type/create_user// normal interface rps=198.01ab-n 10000-c 150-p'/ tmp/ab_post_data.json'-T 'application:json'-r http://www.laravel_type_test.com/api/common/create_user/***** the fifth * / type qualified interface rps=191.17ab-n 100000-c 150- P'/ tmp/ab_post_data.json'-T 'application:json'-r http://www.laravel_type_test.com/api/type/create_user// general interface rps=190.55ab-n 100000-c 150-p' / tmp/ab_post_data.json'-T 'application:json'-r http://www.laravel_type_test.com/api/common/create_user(4) result

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

Type qualification does not improve performance as much as expected, very small, but it is recommended.

These are all the contents of this article entitled "does PHP7 function type qualification affect performance?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Type interface general parameter function performance influence result content name article error recommendation support configuration rigorous small consistent for reference only operating system Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Tech Info Linux vpn NVidia