Baidu engineers talk about the implementation principle and performance analysis of PHP function (II.)

Source: Internet
Author: User
Tags arithmetic comparison hash types of functions zend

This article mainly introduces the Baidu engineer to talk about the implementation of PHP functions and performance Analysis (ii), this article explains the class method, performance comparison, built-in functions and user function performance comparisons, and so on, the need for friends can refer to the

Class method

The execution principle of the class method is the same as that of the user function, and it is also translated into opcodes sequential invocation. The implementation of the class, Zend with a data structure zend_class_entry, which holds some basic information related to the class. This entry was done when PHP was compiled.

In the common of zend_function, one member is called scope, which points to the zend_class_entry of the corresponding class of the current method. 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 implementation of the principle of object-oriented PHP. As far as function is concerned, the method implementation principle and function are identical, theoretically its performance is similar, we will do a detailed performance comparison.

Performance comparison

Effect of function name length on performance

The test method compares the functions of name length 1, 2, 4, 8, 16, tests the number of times they can be executed per second, and determines the effect of function name length on performance

The results of the test are shown below

"Analysis of the results

As you can see from the diagram, the length of the function name has a certain effect on performance. A function with a length of 1 and an empty function call of length 16 are 1 time times worse performance. Analysis of the source is not difficult to find reasons, as described earlier, function call Zend will first in a global funtion_table through the function name query relevant information, function_table is a hash table. Inevitably, the longer the name, the more time it takes to query. Therefore, when you actually write the program, the name of the function that is called many times is not recommended too long.

Although the function name length has some effect on performance, but how much is it? This problem should still be considered in combination with the actual situation, if a function itself is more complex, then the overall performance impact is not large. One suggestion is that for those functions that can be invoked many times, and that their functions are simpler, a few concise names can be appropriately taken.

The effect of number of functions on performance

"" Test method

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

The results of the test are shown below

"Analysis of the results

From the test results can be seen in these three cases, performance is almost the same, the number of functions to increase the performance drop negligible, can be ignored. From the realization principle analysis, the only difference between several implementations lies in the part of function acquisition. As mentioned above, all functions are placed in a hash table, the search efficiency in different numbers should be close to O (1), so the performance gap is small.

Different types of function call consumption

"" Test method

Select the user function, class method, static method, built-in function of each one, the function itself does nothing, directly returned, mainly test the consumption of empty function calls. Test results for every second of the executable number of tests in order to remove other effects, all function names are the same length

The results of the test are shown below

"Analysis of the results

Through the test results can be seen, for the user to write their own PHP functions, regardless of which type, its efficiency is similar, all in 280w/s or so. As we expect, even the air-conditioning, the built-in function of its efficiency is much higher, to 780w/s, is 3 times times the former. Visible, the overhead of a built-in function call is far below the user's function. From the front principle analysis, we know that the main difference lies in the initialization of symbol table and receiving parameters in user function call.

Performance comparisons between built-in functions and user functions

"" Test method

The performance comparison between the built-in function and the user function, here we select several commonly used functions, and then use PHP to implement the function of the same function for performance comparison. In the test, we select the string, mathematics, and the array of each typical comparison, these functions are string interception (SUBSTR), 10 to 2 (decbin), Minimum (min) and return the array of the key (Array_keys).

The results of the test are shown below

"Analysis of the results

As you can see from the test results, as we expected, built-in functions are much higher in overall performance than normal user functions. In particular, for functions involving string-type operations, the gap reached 1 orders of magnitude. Therefore, one principle that functions use is that if a function has a built-in function, try to use it instead of writing PHP functions yourself. For a number of functions involving a large number of string operations, to improve performance, you can consider using extensions to implement. such as the common rich text filtering and so on.

Performance comparison with C function

"" Test method

We choose string manipulation and arithmetic operations each of the 3 functions for comparison, PHP with the extension implementation. Three kinds of functions are simple algorithm operation, string comparison and multiple arithmetic operations. In addition to its own two types of functions, but also to test the function of the cost of air conditioning to remove the performance, on the one hand compared to the two functions (C and PHP built-in) their own performance differences, in addition to the side of the air conditioning function consumption test point for the execution of 10w time consumption

The results of the test are shown below

"Analysis of the results

The overhead of the built-in functions and the C function is less significant after removing the effect of the PHP function, and as function becomes more complex, the performance of both sides tends to be the same. This is also easy to prove from the previous function implementation analysis, after all, the built-in function is C implementation. The more complex function functions, C and PHP performance gap between the smaller relative to C, PHP function calls a lot of overhead, for simple functions, performance still has a certain impact. Therefore, the function in PHP is not suitable for nested encapsulation too deep.

Pseudo function and its performance

In PHP, there are some functions that they are used as standard functional usages, but the underlying implementations are completely different from the real function calls, which are not part of any of the three functions mentioned above, and are essentially a separate opcode, which is called pseudo function or instruction function.

As mentioned above, pseudo functions do not use the same as the standard function, and appear to have the same characteristics. But they are finally executed by Zend reflected into a corresponding instruction (opcode) to invoke, so its implementation is closer to if, for, arithmetic operations.

"The Pseudo function in PHP

Isset

Empty

unset

Eval

As can be seen from the above introduction, the pseudo function is better than the overhead of a function call because it is translated directly into instructions, which is less expensive than a normal function. We do a comparison with the following tests. Both Array_key_exists and isset can determine whether a key exists in the array, and look at their performance

As can be seen from the graph, 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 call, its performance is about 1 time times higher. This also side proof again explained the PHP function call overhead is 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.