Baidu engineers talk about the implementation principle and performance analysis of PHP function (ii), PHP function _php Tutorial

Source: Internet
Author: User
Tags types of functions

Baidu engineers talk about the implementation principle and performance analysis of PHP function (ii), PHP function


Class method
The class method is executed in the same way as the user function, and is also translated into opcodes sequential invocation. Class implementation, Zend with a data structure zend_class_entry to implement, which holds some basic information related to the class. This entry is already processed when PHP is compiled.
In Zend_function's common, there is a member called scope, which points to the zend_class_entry of the current method's corresponding class. About the object-oriented implementation of PHP, here is not to do more detailed introduction, in the future will be dedicated to write an article to detail the object-oriented implementation of PHP principles. As far as the function is concerned, the method implementation principle and function are exactly the same, in theory its performance is similar, we will do detailed performance comparison later.

Performance comparison
Effect of function name length on performance

The test method compares the functions of 1, 2, 4, 8, 16, and tests the number of executable times per second to determine the effect of function name length on performance.

"Test results such as

"Results analysis
As can be seen from the figure, the length of the function name has a certain effect on performance. A function with a length of 1 and an empty function call with a length of 16 has a performance difference of 1 time times. Analysis of the source code is not difficult to find the reason, as described in the previous statement, when the function call Zend will first in a global funtion_table through the function name query related information, function_table is a hash table. Inevitably, the longer the name, the more time it takes to query. Therefore, in the actual writing program, the function of multiple calls, the name is not recommended too long.

Although function name length has some effect on performance, how big is it? This problem should still be combined with the actual situation to consider, if a function itself is more complex, then the overall performance impact is not big. One suggestion is that for those functions that will be called many times, their functions are relatively simple, and some concise names can be properly taken.
The effect of the number of functions on performance

"Test method
Function call test in the following three environments, analysis results: 1. The program contains only 1 function 2. The program contains 100 functions 3. The program contains 1000 functions. Test the number of functions that can be called per second in these three cases

"Test results such as

"Results analysis
From the test results, it can be seen that the performance is almost the same in these three cases, and the performance decrease is negligible when the number of functions increases. From the realization principle analysis, the only difference in several implementations is the part that the function obtains. As mentioned above, all functions are placed in a hash table, the search efficiency under different numbers should be close to O (1), so the performance gap is small.
Different types of function call consumption
"Test method
Choose User function, class method, static method, built-in function each one, the function itself does not do anything, direct return, main test empty function call consumption. The test result is the number of executable times per second tested to remove other effects, all function names are the same length
"Test results such as

"Results analysis
Through the test results can be seen, for the user to write their own PHP functions, regardless of which type, its efficiency is similar, are around 280w/s. As we expected, even air-conditioning, built-in functions are much more efficient, to reach 780w/s, is 3 times times the former. It can be seen that the cost of built-in function calls is much lower than the user function. From the previous principle analysis, we know that the main difference lies in the initialization of the symbol table, receiving parameters and other operations when the user function is called.

Performance comparison of built-in functions and user functions

"Test method
The performance comparison of built-in functions and user functions, here we select a few common functions, and then use PHP to implement the same function of the performance comparison. In the test, we select a string, a mathematical, a typical array of comparisons, these functions are string interception (substr), 10 binary to 2 (decbin), the Minimum (min) and the return array, so key (Array_keys).
"Test results such as

"Results analysis
As you can see from the test results, the built-in functions, as we expect, are much higher in overall performance than ordinary user functions . Especially for functions involving string class operations, the gap reaches 1 orders of magnitude. Therefore, one of the principles used by the function is that if a function has a corresponding built-in function, try to use it instead of writing PHP functions yourself. For some features that involve a lot of string manipulation, you might want to consider scaling to improve performance. such as the common rich text filter.
and C function Performance comparison

"Test method
We select the string manipulation and arithmetic operations of each of the 3 functions of the comparison, PHP with the extension implementation. Three functions are simple one-time algorithm operation, string comparison and multiple algorithm operation. In addition to their own two types of functions, will also test the function of the cost of air conditioning after the performance, on the one hand compared to the two functions (C and PHP built-in) the performance of the difference, the other is the side of the air conditioning function consumption test point to perform 10w operation time consumption
"Test results such as

"Results analysis
The overhead of the built-in functions and the C-functions is less than the effect of the PHP function, and as functions become more complex, the performance of the two sides approaches the same. This is also easily demonstrated in the previous function implementation analysis, after all, the built-in function is C implementation. The more complex the function, the smaller the performance gap between C and PHP. The cost of PHP function calls is much higher relative to C, which has a certain effect on simple functions. Therefore, PHP functions should not be nested too deep in the package.
Pseudo function and its performance

In PHP, there are functions that are standard function usages in use, but the underlying implementations are completely different from the actual function calls, which are not part of any of the three functions mentioned earlier, and are essentially a separate opcode, which is called pseudo functions or instruction functions.

As stated above, pseudo-functions are used with standard functions and appear to have the same characteristics. But they are finally executed by the Zend reflected as a corresponding instruction (opcode) to invoke, so its implementation is closer to the if, for, arithmetic operations and other operations.
"The Pseudo function in PHP
Isset
Empty
unset
Eval
As can be seen from the above, the pseudo-function is executed by direct translation into instruction, and the overhead of a function call is less than the normal function, so performance is better. Let's make a comparison by testing the following. Both Array_key_exists and isset can tell if a key exists in the array and see their performance

As can be seen from the figure, compared with array_key_exists, isset performance is much higher, the basic is about 4 times times the former, and even compared with the empty function, its performance is also about 1 time times higher. This also proves that the cost of PHP function calls is still relatively large.

http://www.bkjia.com/PHPjc/998811.html www.bkjia.com true http://www.bkjia.com/PHPjc/998811.html techarticle Baidu Engineers talk about the implementation principle and performance analysis of PHP function (ii), the PHP function class method class method whose execution principle and user function is the same, but also translated into opcodes sequential call. Class ...

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