In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the implementation principle and performance analysis of common functions in PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Implementation and introduction of commonly used php functions
Count
Count is a function we often use to return the length of an array.
What is the complexity of the function count? A common saying is that the count function traverses the entire array and calculates the number of elements, so the complexity is O (n). Is this the actual situation? Let's go back to the implementation of count. Through the source code, we can find that for the count operation of the array, the final path of the function is zif_count- > php_count_recursive- > zend_hash_num_elements, while the behavior of zend_hash_num_elements is return ht- > nNumOfElements, which shows that this is an O (1) rather than O (n) operation. In fact, the array is a hash_table at the bottom of the php. For the hash table, there is a special element nNumOfElements in the zend that records the number of current elements, so for a normal count, it actually returns this value directly. From this, we come to the conclusion that count is the complexity of O (1) and has nothing to do with the size of the specific array.
For variables that are not array types, how does count behave? Returns 0 for unset variables, while 1 for int, double, string, etc.
Strlen
Strlen is used to return the length of a string. So what is the principle of his implementation? We all know that strlen is an o (n) function in c, traversing the string sequentially until it encounters\ 0, and then gives the length. Is this the same in Php? The answer is no, the string in php is described by a compound structure, including pointers to specific data and string length (similar to string in C++), so strlen directly returns the string length, which is a constant-level operation. In addition, for non-string type variables to call strlen, it will first cast the variable to a string and then find the length, which should be noted.
Isset and array_key_exists
The most common use of both functions is to determine whether a key exists in an array. But the former can also be used to determine whether a variable has been set. As mentioned earlier, isset is not a real function, so it will be much more efficient than the latter. It is recommended instead of array_key_exists.
Array_push and array []
Both append an element to the end of the array. The difference is that the former can push multiple at a time. The biggest difference between them is that one is a function and the other is a language structure, so the latter is more efficient. So if it's just a normal append element, it's recommended to use array [].
Rand and mt_rand
Both provide the ability to generate random numbers, the former using the rand of the libc standard. The latter uses the known characteristics in Mersenne Twister as the random number generator, which can generate random values at an average speed of four times faster than the rand () provided by libc. Therefore, if you have high performance requirements, you can consider using mt_rand instead of the former. As we all know, rand produces pseudorandom numbers, and in C # you need to use srand to display the specified seeds. But in php, rand will call srand for you by default, and usually you don't need to display the call again. It is important to note that if you need to call srand under special circumstances, be sure to call it. That is to say, srand must not be mixed with srand corresponding to rand,mt_srand, otherwise it is invalid.
Sort and usort
Both are used for sorting, except that the former can specify a sorting strategy, similar to qsort in our C and sort of C++. Both of them are implemented by standard fast scheduling. For those with sorting requirements, such as non-special cases, you can call these methods provided by php. You don't have to implement them again, and the efficiency will be much lower. For the reasons, see the previous analysis and comparison of user functions and built-in functions.
Urlencode and rawurlencode
Both of these are used for url encoding, except for-_. All non-alphanumeric characters outside will be replaced with a percent sign (%) followed by two hexadecimal digits. The only difference between the two is that for spaces, urlencode encodes as + and rawurlencode encodes as% 20. In general, except for search engines, our strategy is to code spaces as% 20. Therefore, the latter is mostly adopted. Note that encode and decode series must be used together.
Strcmp series functions
This series of functions include strcmp, strncmp, strcasecmp, and strncasecmp, which implement the same functions as C functions. But there is also a difference, because the string of php allows\ 0 to appear, so the underlying layer uses the memcmp series instead of strcmp, which is theoretically faster. In addition, because php can get the string length directly, it will be checked first, and in many cases it will be much more efficient.
Is_int and is_numeric
The functions of these two functions are similar but not exactly the same, so you must pay attention to the difference between them when using them. Is_int: determine whether a variable type is an integer type, php variables have a special field to represent the type, so directly determine this type, is an absolute O (1) operation Is_numeric: determine whether a variable is an integer or numeric string, that is to say, in addition to integer variables will return true, for string variables, such as "1234", "1e4" and so on will also be judged as true. At this time, the string will be traversed to judge.
Summary and suggestion
Summary:
Through the principle analysis and performance test of function implementation, we conclude the following conclusions.
1. The function call cost of Php is relatively high.
2. The function-related information is stored in a large hash_table, and each call is looked up in the hash table through the function name, so the length of the function name also has an impact on performance.
3. The function return reference has no practical significance.
4. The performance of the built-in php function is much higher than that of user functions, especially for string operations.
5. The efficiency of class method, ordinary function and static method is almost the same, and there is not much difference.
6. excluding the influence of empty function calls, the performance of built-in functions is basically similar to that of C functions with the same function.
7. All parameters are passed with shallow copies of reference counting, with little cost.
8. The effect of the number of functions on performance is almost negligible.
Recommendations:
Therefore, there are some suggestions for the use of the php function
1. A function can be done with a built-in function, try to use it instead of writing your own php function.
2. If a feature requires high performance, you can consider using extensions to implement it.
3. Php function calls are expensive, so don't over-encapsulate them. Some functions, if you need to call a lot of times and only use 1 or 2 lines of code to achieve, it is recommended not to encapsulate the call.
4. don't be too obsessed with various design patterns, as described in the previous article, excessive encapsulation will lead to performance degradation. The tradeoff between the two needs to be considered. Php has its own characteristics, so we must not imitate the model of java excessively.
5. Functions should not be nested too deeply, and recursion should be used carefully.
6. Pseudo-function has high performance, and priority is given to the implementation of the same function. For example, using isset instead of array_key_exists
7. The function return reference does not make much sense, nor can it play a practical role, so it is recommended not to consider it.
8. Class member methods are no less efficient than ordinary functions, so you don't have to worry about performance loss. It is recommended to consider more static methods for better readability and security.
9. If it is not a special need, it is recommended to pass a value instead of a reference in parameter passing. Of course, if the parameters are a large array and need to be modified, you can consider passing references.
This is the end of the content of "implementation principle and performance Analysis of PHP Common functions". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.