Post to share) php function summary and suggestions

Source: Internet
Author: User
(Post for sharing) php function summary and suggestions through the analysis of function implementation principles and performance tests, we conclude that PHP function calling overhead is relatively large. Function-related information is stored in a large hash_table. during each call, the function name is searched in the hash table. Therefore, the function name length also affects the performance. The reference returned by the function has no practical significance. Summary and suggestions of php functions with built-in php functions
Through analyzing the principles of function implementation and testing the performance, we have summarized the following conclusions:

PHP has relatively high function call overhead.
Function-related information is stored in a large hash_table. during each call, the function name is searched in the hash table. Therefore, the function name length also affects the performance.
The reference returned by the function has no practical significance.
Built-in php functions provide much higher performance than user functions, especially for string operations.
Class methods, common functions, and static methods have almost the same efficiency, but there is no big difference.
Apart from the impact of empty function calls, the performance of built-in functions is similar to that of C functions of the same function.
All parameter passing uses the reference count for the shortest copy at a low cost.
The number of functions has almost no impact on performance.
Therefore, we have the following suggestions for using php functions:

A function can be completed using a built-in function. try to use it instead of writing a php function by yourself.
If a feature has high performance requirements, you can consider using extensions.
PHP function calls are costly and therefore should not be overly encapsulated. Some functions, if you need to call a lot of times itself and only use 1 or 2 lines of code to implement it, we recommend that you do not encapsulate the call.
Do not be overly infatuated with various design patterns. as described above, excessive encapsulation may lead to a reduction in performance. Consider the trade-off between the two. PHP has its own characteristics, so it is not feasible to follow the java mode too much.
Functions should not be nested too deeply, so be cautious when using recursion.
The performance of pseudo functions is very high, and the implementation of the same function is preferred. For example, use isset instead of array_key_exists.
The function return reference does not make much sense and does not play a practical role. we recommend that you do not consider it.
Class Member methods are less efficient than common functions, so you don't have to worry about performance loss. We recommend that you consider static methods for better readability and security.
If this is not a special requirement, we recommend that you pass the parameter values instead of references. Of course, if the parameter is a large array and needs to be modified, you can consider the reference transfer.

------ Solution --------------------
The author takes it for granted.
The hash value is a number rather than a string. in the hash table, it indicates the offset of the item relative to the header. Therefore, the query efficiency is very high. The function name has been converted to the hash value during pre-Compilation. In fact, all php extension functions are in this table.

Not to mention it. from the author's description, we can:
Do not use frameworks
Do not write classes by yourself
Do not define functions by yourself
If the required function is not provided by php, discard the php


------ Solution --------------------
There is no optimal thing. If possible, it is the best.

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.