PHP code optimization-Comparison of member variable acquisition speed

Source: Internet
Author: User
This article mainly introduces the comparison of the retrieval speed of PHP class member variables in four ways, and analyzes the causes in detail, for more information, see the following four sample codes. what do you think is the sorting speed of object creation and member variables?

1: Set the member variable to public. assign a value to the member variable through the value assignment operation to directly obtain the variable.

The code is as follows:


Class Foo {
Public $ id;
}
$ Data = new Foo;
$ Data-> id = 10;
Echo $ data-> id;
?>


2: Set the member variable to public. you can use the constructor to set the value of the member variable and directly obtain the variable.

The code is as follows:


Class Foo2 {
Public $ id;
Public function _ construct ($ id ){
$ This-> id = $ id;
}
}

$ Data = new Foo2 (10 );
Echo $ data-> id;
?>


3: Set the member variable to protected, set the value of the member variable through the constructor, and obtain the variable through magic

The code is as follows:


Class Foo3 {
Protected $ id;
Public function _ construct ($ id ){
$ This-> id = $ id;
}

Public function getId (){
Return $ this-> id;
}
}
$ Data = new Foo3 (10 );
Echo $ data-> getId ();
?>


4: Set the member variable to protected, set the value of the member variable through the constructor, and get the variable through the member method
Class Foo4 {
Protected $ id;
Public function _ construct ($ id ){
$ This-> id = $ id;
}

Public function _ get ($ key ){
Return $ this-> id;
}
}
$ Data = new Foo4 (10 );
Echo $ data-> id;
?>
Sort by execution speed: 1243
Let's first look at its opcode:
1:

The 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 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 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 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 and its opcode implementation in the zend_vm_execute.h file, what can we find?

I. PHP kernel object creation process is divided into three steps:

ZEND_FETCH_CLASS obtains the storage class variables based on the class name and implements a search operation for hashtalbe EG (class_table ).
NEW initializes the object and points EX (call)-> fbc to the constructor pointer.
Call the constructor. the call is the same as that of other functions. they call zend_do_fcall_common_helper_SPEC.

2. the magic method is triggered by a condition instead of a direct call. for example, the member variable id in our example is obtained.

(Zend_std_read_property). The steps are as follows:
Obtain the attributes of an object. if the object exists, go to step 2. if there is no relevant attribute, go to step 3.
Check whether the property corresponding to the name exists from the properties of the object. if the returned result exists and does not exist, go to step 3.
If the _ get magic method exists, call this method to obtain the variable. if it does not exist, an error is returned.
Back to sorting:

1. what is the difference between the first and second?

The second opcode is less than the first one, but slower than the first one, because the constructor has more parameters and one more parameter to process opcode. Parameter processing is a time-consuming operation. when code optimization is performed, some unnecessary parameters can be removed. when a function has multiple parameters, you can use an array to encapsulate it and pass it in.

2. Why is the third slowest?

Because its parameter acquisition is essentially a call to the object member method, the call cost of the method is higher than that of the variable.

3. Why is the fourth one faster than the third one?

Because the fourth operation actually obtains the variable, but calls the magic method internally, the call efficiency of the internal function is higher than that of the user-defined method. Therefore, when some PHP kernel implementation methods can be called, do not reinvent the wheel.
4. Why is the fourth one slower than the second one?
Because when a PHP object obtains a variable, when the member variable is not defined in the class, it calls PHP's unique magic method _ get, A magic method call is added.

Summary:

1. use PHP built-in functions
2. not object-oriented (OOP). object-oriented usually has a high overhead, and each method and object call consumes a lot of memory.
3. try to use less magic methods-do not use the framework unless necessary, because the framework has a lot of magic methods to use.
4. in performance-first application scenarios, it is better to use member variables when you need to use OOP.
5. do not use functions that can use PHP syntax structures. do not write built-in functions by yourself. do not use objects that can use functions.
Related Article

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.