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

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

Jul 13, 2016 am 09:53 AM
php function

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

This article mainly introduces Baidu engineers’ talk about the implementation principles and performance analysis of PHP functions (2). This article explains class methods, performance comparison, performance comparison of built-in functions and user functions, etc. Friends in need can refer to it

Class method

The execution principle of class methods is the same as that of user functions, and they are also translated into opcodes and called sequentially. Class implementation is implemented by zend using a data structure zend_class_entry, which stores some basic information related to the class. This entry is processed when PHP is compiled.

In the common of zend_function, there is a member called scope, which points to the zend_class_entry of the class corresponding to the current method. Regarding the object-oriented implementation in PHP, I will not give a more detailed introduction here. In the future, I will write a special article to detail the object-oriented implementation principle in PHP. As far as the function is concerned, the implementation principle of method is exactly the same as that of function, and its performance is similar in theory. We will make a detailed performance comparison later.

Performance comparison

The impact of function name length on performance

》》Test method Compare functions with name lengths of 1, 2, 4, 8, and 16, test and compare the number of times they can be executed per second, and determine the impact of function name length on performance

》》The test results are as shown below

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

》》Result Analysis

As can be seen from the figure, the length of the function name still has a certain impact on performance. A function of length 1 and an empty function call of length 16 have a performance difference of 1x. It is not difficult to find the reason by analyzing the source code. As mentioned above, when a function is called, zend will first query relevant information by function name in a global function_table, which is a hash table. Inevitably, the longer the name, the more time it takes to query. Therefore, when actually writing a program, it is recommended that the name of a function that is called multiple times should not be too long.

Although the length of the function name has a certain impact on performance, how big is it specifically? This issue should still be considered based on the actual situation. If a function itself is relatively complex, it will not have a big impact on the overall performance. One suggestion is to give concise and concise names to functions that are called many times and have relatively simple functions.

The impact of the number of functions on performance

》》Test method

Conduct function call tests in the following three environments, and analyze the 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 situations

》》The test results are as shown below

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

》》Result Analysis

It can be seen from the test results that the performance in these three cases is almost the same. When the number of functions increases, the performance decrease is minimal and can be ignored. From the analysis of implementation principles, the only difference between several implementations is the function acquisition part. As mentioned before, all functions are placed in a hash table, and the search efficiency should still be close to O(1) under different numbers, so the performance difference is not big.

Different types of function call consumption

》》Test method

Select one of user functions, class methods, static methods, and built-in functions. The function itself does not do anything and returns directly. It mainly tests the consumption of empty function calls. The test results are the number of executions per second. In order to remove other effects during the test, all function names have the same length

》》The test results are as shown below

》》Result Analysis

It can be seen from the test results that for the PHP functions written by users themselves, no matter what type they are, their efficiency is almost the same, all around 280w/s. As we expected, even for air conditioners, the efficiency of the built-in function is much higher, reaching 780w/s, which is three times that of the former. It can be seen that the overhead of built-in function calls is still much lower than that of user functions. From the previous principle analysis, it can be seen that the main gap lies in operations such as initializing the symbol table and receiving parameters when the user function is called.

Performance comparison between built-in functions and user functions

》》Test method

Performance comparison of built-in functions and user functions. Here we select several commonly used functions, and then use PHP functions to implement the same functions to compare the performance. During the test, we selected a typical one from each of strings, mathematics, and arrays for comparison. These functions are string interception (substr), decimal conversion to binary (decbin), minimum value (min), and return. All keys in the array (array_keys).

》》The test results are as shown below

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

》》Result Analysis

From the test results, we can see that, as we expected, the overall performance of built-in functions is much higher than that of ordinary user functions. Especially for functions involving string operations, the gap reaches 1 order of magnitude. Therefore, one principle for using functions is that if a certain function has a corresponding built-in function, try to use it instead of writing the PHP function yourself. For some functions involving a large number of string operations, in order to improve performance, you can consider using extensions. For example, common rich text filtering, etc.

Performance comparison with C function

》》Test method

We selected three functions each for string operations and arithmetic operations for comparison, and PHP was implemented using extensions. The three functions are simple one-time arithmetic operations, string comparisons, and multiple arithmetic operations. In addition to the two types of functions, we will also test the performance after removing the function air conditioning overhead. On the one hand, we compare the performance differences of the two functions (C and PHP built-in), and on the other hand, we verify the consumption test points of the air conditioning function: Time consumption to perform 100,000 operations

》》The test results are as shown below

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

》》Result Analysis

The difference between the overhead of built-in functions and C functions is small after removing the impact of php function air conditioning. As the functions become more and more complex, the performance of both parties approaches the same. This can be easily demonstrated from the previous function implementation analysis. After all, the built-in functions are implemented in C. The more complex the function, the smaller the performance gap between C and PHP. Compared with C, the overhead of PHP function calls is much higher, and the performance of simple functions still has a certain impact. Therefore, functions in PHP should not be nested and encapsulated too deeply.

Pseudo functions and their performance

In PHP, there are some functions that are standard function usage, but the underlying implementation is completely different from the real function call. These functions do not belong to any of the three functions mentioned above. Its essence is a separate opcode, which is called a pseudo function or instruction function here.

As mentioned above, pseudo functions are used just like standard functions and appear to have the same characteristics. But when they are finally executed, they are reflected by zend into a corresponding instruction (opcode) for calling, so their implementation is closer to operations such as if, for, and arithmetic operations.

》》Pseudo functions in php

isset

empty

unset

eval

From the above introduction, it can be seen that since pseudo functions are directly translated into instructions for execution, compared with ordinary functions, there is one less overhead caused by a function call, so the performance will be better. We make a comparison through the following test. Both Array_key_exists and isset can determine whether a key exists in the array. Let’s take a look at their performance

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

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000085.htmlTechArticleBaidu engineers talk about the implementation principles and performance analysis of PHP functions (2) This article mainly introduces Baidu engineers talking about PHP Function implementation principles and performance analysis (2), this article explains class methods,...
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 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

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.

See all articles