In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the differences in the acquisition speed of member variables in PHP code optimization". In daily operations, I believe that many people have doubts about the differences in the acquisition speed of member variables in PHP code optimization. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the differences in the speed of obtaining member variables in PHP code optimization?" Next, please follow the editor to study!
Here are four code examples. what do you think is the speed in which they create objects and get member variables?
1: set the member variable to public, assign a value to the member variable through the assignment operation, and get the variable directly
The copy code is as follows:
2: set the member variable to public, set the value of the member variable through the constructor, and get the variable directly.
The copy code is as follows:
3: set the member variable to protected, set the value of the member variable through the constructor, and obtain the variable by magic.
The copy code is as follows:
4: set the member variable to protected, set the value of the member variable through the constructor, and obtain the variable through the member method.
Sort by execution speed: 1243
Let's first look at its opcode:
1:
The copy code is as follows:
1 ZEND_FETCH_CLASS 4: 4 'Foo'
2 NEW $5: 4
3 DO_FCALL_BY_NAME 0
4 ASSIGN! 0, $5
5 ZEND_ASSIGN_OBJ! 0, 'id'
6 ZEND_OP_DATA 10
7 FETCH_OBJ_R $9! 0, 'id'
8 ECHO $9
2:
The copy code is as follows:
1 ZEND_FETCH_CLASS 4: 10 'Foo2'
2 NEW $11: 10
3 SEND_VAL 10
4 DO_FCALL_BY_NAME 1
5 ASSIGN! 1, $11
6 FETCH_OBJ_R $14! 1, 'id'
7 ECHO $14
3:
The copy code is as follows:
1 ZEND_FETCH_CLASS 4: 15 'Foo3'
2 NEW $16: 15
3 SEND_VAL 10
4 DO_FCALL_BY_NAME 1
5 ASSIGN! 2, $16
6 ZEND_INIT_METHOD_CALL! 2, 'getId'
7 DO_FCALL_BY_NAME 0 $20
8 ECHO $20
4:
The copy code is as follows
1 ZEND_FETCH_CLASS 4: 21 'Foo4'
2 NEW $22: 21
3 END_VAL 10
4 DO_FCALL_BY_NAME 1
5 ASSIGN! 3, $22
6 FETCH_OBJ_R $25! 3, 'id'
7 ECHO $25
Based on the opcode above, what can we find with reference to its corresponding opcode implementation in the zend_vm_execute.h file?
1. The process of creating objects in the PHP kernel is divided into three steps:
ZEND_FETCH_CLASS gets the variable that stores the class according to the class name, which is implemented as a hashtalbe EG (class_table) lookup operation
NEW initializes the object, pointing EX (call)-> fbc to the constructor pointer.
Call the constructor, which, like any other function call, calls zend_do_fcall_common_helper_SPEC
Second, the call to the magic method is triggered by conditions, not directly, such as the acquisition of the member variable id in our example.
(zend_std_read_property), whose steps are:
Get the properties of the object, if they exist, go to the second step; if there are no related attributes, go to the third step
Look for the existence of a property corresponding to the name from the properties of the object. If so, return the result. If not, go to step 3.
If there is a _ _ get magic method, call this method to get the variable. If it does not exist, an error will be reported.
Back to the question of sorting:
What's the difference between the first one and the second one?
The second has less opcode than the first, but is slower than the first, because the constructor has more arguments and one more opcode to handle parameters. Parameter processing is a time-consuming operation, when we are doing code optimization, some unnecessary parameters can be removed; when a function has multiple parameters, we can consider encapsulating it through an array and passing it in.
Second, why is the third the slowest?
Because the acquisition parameter is essentially a call to the object member method, the calling cost of the method is higher than that of the variable.
Why is the fourth faster than the third?
Because the fourth operation essentially gets the variable, but its internal implementation of the magic method call, compared to the user-defined method, the internal function call will be more efficient. So don't reinvent the wheel when we have some methods implemented by the PHP kernel to call.
Why is the fourth slower than the second?
Because in the PHP object to obtain variables in the process, when the member variable in the definition of the class is not available, will call the PHP-specific magic method _ _ get, there is one more magic method call.
To sum up:
1. Use PHP built-in functions
two。 It is not necessary to be object-oriented (OOP). Object-oriented is often very expensive, and each method and object call consumes a lot of memory.
3. Minimize the use of magic methods-do not use frames unless necessary, because frames are used by a large number of magic methods.
4. In performance-first scenarios, it's a good idea to use member variables when you need to use OOP.
5. Don't use functions if you can use PHP syntax structure, don't write by yourself if you can use built-in functions, and don't use objects if you can use functions.
At this point, the study on "what are the differences in the acquisition speed of member variables in PHP code optimization" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.