Baidu engineers talk about the implementation principle and performance analysis of PHP functions (2). php Functions

Source: Internet
Author: User
Tags types of functions

Baidu engineers talk about the implementation principle and performance analysis of PHP functions (2). php Functions

Class Method
Its execution principle is the same as that of user functions, and it is also translated into opcodes for sequential calls. Class implementation, zend is implemented using a Data Structure zend_class_entry, which stores some basic information related to the class. This entry has been processed during php compilation.
In the common of zend_function, a member is called scope, which points to the zend_class_entry of the class corresponding to the current method. For the implementation of object-oriented in php, I will not introduce it in detail here. In the future, I will write a special article to detail the implementation principles of object-oriented in php. For the function, the implementation principle of method is the same as that of function, and its performance is similar in theory. We will make a detailed performance comparison later.

Performance Comparison
Impact of Function Name Length on Performance

"Test method compare functions with names of 1, 2, 4, 8, and 16, test and compare the number of executions per second, and determine the impact of Function Name Length on Performance

"Test results are as follows:

"Result Analysis
We can see from the figure that the length of the function name still has a certain impact on the performance. A function with a length of 1 and an empty function with a length of 16 have a performance difference of 1 times. Analyzing the source code is not difficult to find the cause, as described above, zend will first query the relevant information through the function name in a global funtion_table during function calling. function_table is a hash table. Inevitably, the longer the name, the more time it takes to query. Therefore, when writing a program, it is recommended that the name of the function be too long for multiple calls.

Although the function name length has a certain impact on the performance, how big is it? This problem should still be considered based on the actual situation. If a function is complicated, it will not affect the overall performance. One suggestion is that some concise names can be used for functions that call many times and have simple functions.
Impact of number of functions on Performance

"Test Method
Test the function call in the following three environments: 1. The program contains only one function. 2. The program contains 100 functions. 3. The program contains 1000 functions. Test the number of function calls per second in these three cases

"Test results are as follows:

"Result Analysis
From the test results, we can see that the performance is almost the same in these three cases. When the number of functions increases, the performance decreases slightly and can be ignored. From the implementation principle analysis, the only difference between several implementations is the function acquisition part. As described above, all functions are placed in a hash table, and the search efficiency should be close to O (1) under different numbers, so the performance gap is not big.
Consumption of different types of function calls
"Test Method
Choose one of user functions, class methods, static methods, and built-in functions. The function itself does nothing and returns directly. It mainly tests the consumption of empty function calls. The test result is the number of executions per second. In this test, the length of all function names is the same.
"Test results are as follows:

"Result Analysis
The test results show that the efficiency of php functions compiled by users is about 280 w/s, regardless of the type. As we expected, even the built-in functions of air conditioners are much more efficient, reaching 780 w/s, which is three times the former. It can be seen that the overhead of built-in function calls is much lower than that of user functions. From the previous analysis, we can see that the main difference lies in the operations such as initializing the symbol table and receiving parameters when the user functions are called.

Performance Comparison between built-in functions and user functions

"Test Method
The Performance Comparison between built-in functions and user functions. Here we select several common functions and use php to implement functions with the same function for performance comparison. In the test, we select a typical string, a mathematical string, and an array for comparison. These functions are string truncation (substr), decimal to decimal (decbin), calculates the minimum value (min) and returns the key (array_keys) in the array ).
"Test results are as follows:

"Result Analysis
The test results show that, as we expected,The overall performance of built-in functions is much higher than that of common functions.. Especially for functions involving string operations, the gap has reached one order of magnitude. Therefore, if a function has a built-in function, try to use it instead of writing the php function by yourself. For some functions that involve a large number of string operations, you can consider using extensions to improve performance. For example, common rich text filtering.
Performance Comparison with C Functions

"Test Method
We select three types of functions for string operation and arithmetic operation for comparison. php is implemented with extension. The three functions are simple algorithm operations, string comparison, and multiple algorithm operations. In addition to the two types of functions, the system also tests the performance after removing the function air-conditioning overhead. On the one hand, it compares the performance differences between the two functions (c and php built-in, in addition, the consumption test points of the air conditioning function are verified on the side to consume 10 million operations.
"Test results are as follows:

"Result Analysis
The overhead of built-in functions and C functions is relatively small after the impact of php function air-conditioning is removed. As function functions become more complex, the performance of both sides approaches the same. This is also easily demonstrated from the previous implementation analysis of functions. After all, the built-in functions are implemented by C. The more complex the function functions, the smaller the performance gap between c and php. Compared with c, the php function call overhead is much larger, which affects the performance of simple functions. Therefore, functions in php should not be nested and encapsulated too deeply.
Pseudo functions and their performance

In php, there are some such functions, which are used in standard function usage, but the underlying implementation is completely different from the real function call, these functions do not belong to any of the three functions mentioned above. Their essence is a separate opcode, which is estimated to be pseudo functions or directive functions.

As mentioned above, pseudo functions use the same features as standard functions. However, during the final execution, zend reflected a corresponding command (opcode) for calling, so its implementation is closer to operations such as if, for, and arithmetic operations.
"Pseudo functions in php
Isset
Empty
Unset
Eval
We can see from the above introduction that the pseudo functions are directly translated into commands for execution, which reduces the overhead of calling the functions than normal functions, so the performance will be better. The following test is used for comparison. Both Array_key_exists and isset can determine whether a key in the array exists and check their performance.

As shown in the figure, the isset performance is much higher than array_key_exists, which is about 4 times higher than that of array_key_exists, and its performance is about twice higher than that of empty function calls. This also proves that the php function call overhead is still relatively large.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.