Table of Contents
Baidu engineers talk about the implementation principles and performance analysis of PHP functions (3)
Home Backend Development PHP Tutorial Baidu engineers talk about the implementation principles and performance analysis of PHP functions (3)_PHP Tutorial

Baidu engineers talk about the implementation principles and performance analysis of PHP functions (3)_PHP Tutorial

Jul 13, 2016 am 09:53 AM
php function

Baidu engineers talk about the implementation principles and performance analysis of PHP functions (3)

This article mainly introduces Baidu engineers’ talk about the implementation principles and performance analysis of PHP functions (3). This article explains the implementation and introduction of commonly used PHP functions, and makes a summary and suggestions. Friends in need can refer to it

Implementation and introduction of commonly used PHP functions

count

Count is a function we often use. Its function is to return the length of an array.

What is the complexity of the count function? A common saying is that the count function will traverse the entire array and then find 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, and the behavior of zend_hash_num_elements is return ht->nNumOfElements, it can be seen that this is an O(1) rather than O(n) operation. In fact, the array is a hash_table at the bottom of PHP. For the hash table, there is a special element nNumOfElements in zend to record the number of current elements, so for general count, this value is actually returned directly. From this, we draw the conclusion: count has a complexity of O(1) and has nothing to do with the size of the specific array.

How does count behave for non-array type variables? For unset variables, it returns 0, while int, double, string, etc. will return 1

strlen

Strlen is used to return the length of a string. So, what is its implementation principle? We all know that strlen is an o(n) function in c, which will sequentially traverse the string until it encounters

isset and array_key_exists

The most common use of these two functions is to determine whether a key exists in the 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 its efficiency will be much higher than the latter. It is recommended to use this instead of array_key_exists.

array_push and array[]

Both of them append an element to the end of the array. The difference is that the former can push multiple ones at one 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. Therefore, if you are just appending elements normally, it is recommended to use array [].

rand and mt_rand

Both provide the function of generating random numbers. The former uses libc standard rand. The latter uses known features in Mersenne Twister as a random number generator, which can generate random values ​​on average four times faster than rand() provided by libc. Therefore, if you have higher performance requirements, you can consider using mt_rand instead of the former. We all know that rand generates pseudo-random numbers. In C, you need to use srand to display the specified seed. But in php, rand will call srand once by default for you. Under normal circumstances, there is no need to explicitly call it yourself. It should be noted that if srand needs to be called under special circumstances, it must be called in conjunction. That is to say, srand corresponds to rand, and mt_srand corresponds to srand. They must not be mixed, otherwise they will be invalid.

sort and usort

Both are used for sorting. The difference is that the former can specify a sorting strategy, similar to our qsort and C sort in C. In terms of sorting, both are implemented using standard quick sort. For those who have sorting requirements, just call these methods provided by PHP unless there are special circumstances. There is no need to re-implement it yourself, and the efficiency will be much lower. The reason can be seen in the previous analysis and comparison of user functions and built-in functions.

Urlencode and rawurlencode

 Both of these are used for url encoding. All non-alphanumeric characters in the string except -_. will be replaced with a percent sign (%) followed by two hexadecimal digits. The only difference between the two is that for spaces, urlencode will be encoded as , while rawurlencode will be encoded as . In general, except for search engines, our strategy is to encode spaces as . Therefore, the latter is mostly used. Note that the encode and decode series must be used together.

strcmp series functions

This series of functions include strcmp, strncmp, strcasecmp, strncasecmp, and their implementation functions are the same as C functions. But there are differences, since php strings are allowed

is_int and is_numeric

The functions of these two functions are similar but not exactly the same. You must pay attention to their differences when using them. Is_int: Determine whether a variable type is an integer type. There is a special field representation type in PHP variables, so you can directly judge this type. It is an absolute O(1) operation. Is_numeric: Determine whether a variable is an integer or a numeric string. , that is to say, in addition to integer variables that will return true, for string variables, if they are in the form of "1234", "1e4", etc., they will also be judged as true. At this time, the string will be traversed for judgment.

Summary and suggestions

Summary:

Through the principle analysis and performance testing of function implementation, we have concluded the following conclusions

 1. Php’s function calling overhead is relatively large.

2. Function-related information is stored in a large hash_table. Each time it is called, the function name is searched in the hash table. Therefore, the length of the function name also has a certain impact on performance.

 3. Function return references have no practical meaning

 4. The performance of built-in PHP functions is much higher than that of user functions, especially for string operations.

 5. The efficiency of class methods, ordinary functions, and static methods is almost the same, there is not much difference

6. Excluding the impact of empty function calls, the performance of built-in functions is basically the same as that of C functions with the same function.

7. All parameter transfers are shallow copies using reference counting, and the cost is very small.

8. The impact of the number of functions on performance is almost negligible

Recommendation:

Therefore, for the use of php functions, there are some suggestions as follows

 1. A function can be completed with a built-in function. Try to use it instead of writing the php function yourself.

2. If a certain function has high performance requirements, you can consider using extensions to implement it.

 3. Php function calls are expensive, so don’t over-encapsulate them. If some functions need to be called a lot and can be implemented with only 1 or 2 lines of code, it is recommended not to encapsulate the calls.

4. Don’t be overly obsessed with various design patterns. As described in the previous article, excessive encapsulation will cause performance degradation. There are trade-offs between the two that need to be considered. Php has its own characteristics, and you must not imitate the Java model too much.

5. Functions should not be nested too deeply, and recursion should be used with caution.

6. Pseudo functions have high performance and will be given priority when implementing the same functions. For example, use isset instead of array_key_exists

 7. Returning a reference from a function does not make much sense and has no practical effect. It is recommended not to consider it.

 8. Class member methods are no less efficient than ordinary functions, so there is no need to worry about performance loss. It is recommended to consider more static methods, which have better readability and security.

9. Unless there is a special need, it is recommended to use pass-by-value instead of pass-by-reference when passing parameters. Of course, if the parameter is a large array and needs to be modified, you can consider passing it by reference.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000086.htmlTechArticleBaidu engineers talk about the implementation principles and performance analysis of PHP functions (3) This article mainly introduces Baidu engineers talking about PHP Function implementation principles and performance analysis (3), this article explains commonly used ph...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles