PHP code optimization member variable acquisition speed comparison _php Tutorial

Source: Internet
Author: User
With the following 4 code examples, what do you think they are creating objects and getting the speed ordering of member variables?

1: Set member variable to public, assign value to member variable by assignment operation, get variable directly
Copy the Code code as follows:
Class Foo {
public $id;
}
$data = new Foo;
$data->id = 10;
Echo $data->id;
?>

2: Set the member variable to public, set the value of the member variable by the constructor, get the variable directly
Copy the Code code 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 by the constructor, get the variable by magic method
Copy the Code code 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, 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 look at its opcode first:
1:
Copy CodeThe 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 zend_assign_obj! 0, ' id '
6 Zend_op_data 10
7 Fetch_obj_r $9! 0, ' id '
8 ECHO $9

2:
Copy CodeThe 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:
Copy CodeThe 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 $
8 ECHO $

4:
Copy CodeThe 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!3, ' id '
7 ECHO


According to the above opcode, what can we find in reference to the implementation of the opcode that corresponds to the zend_vm_execute.h file?

The process of creating an object from the PHP kernel is divided into three steps:

Zend_fetch_class gets the variable of the storage class according to the class name, in fact it is now a hashtalbe EG (class_table) lookup operation
NEW initializes the object, pointing ex (call)->FBC to the constructor pointer.
Call the constructor, whose invocation is the same as any other function call, which is called Zend_do_fcall_common_helper_spec

Second, the Magic method call is triggered by the condition, not directly called, such as our example of the member variable ID gets

(Zend_std_read_property), the steps are:
Gets the properties of the object, if present, to the second step; if there are no related properties, go to step three
Find out if there is a property corresponding to the name from the object's properties, if there is a return result, if it does not exist, go to step three
If there is a __get magic method, call this method to get the variable, if it does not exist, an error
Back to the sort of question:

What is the difference between a first and a second?

The second opcode is less than the first one, but slower than the first, because the constructor has more parameters and a parameter-handling opcode. Parameter processing is a relatively time-consuming operation, when we are doing code optimization, some unnecessary parameters can be removed and removed; When a function has multiple parameters, it can be considered to be passed in by enclosing an array.

Second, why the third slowest?

Because its fetch parameter is essentially a call to an object member method, the method's call cost is higher than the variable's fetch

Third, why is the fourth faster than the other one?

Because the fourth operation essentially gets a variable, it only implements the call to the Magic method internally, and the invocation of the inner function is more efficient than the user-defined method. Therefore, when we have some methods that the PHP kernel implements can be called, do not reinvent the wheel.
Iv. Why is the fourth slower than the second one?
Because in the process of getting the variable in PHP object, when the member variable is not in the definition of the class, it will call PHP's peculiar magic method __get, and one more call of magic method.

To summarize:

1. Using PHP built-in functions
2. Not necessarily object-oriented (OOP), object-oriented tends to be expensive, and each method and object invocation consumes a lot of memory.
3. Try to use less magic methods-unless necessary, do not use frames, because the framework has a lot of magic methods to use.
4. In a performance-first scenario, it's a good idea to have member variables when you need to use OOP.
5. You can use the PHP syntax structure do not use the function, can use the built-in functions do not write their own, can use the function of the object

http://www.bkjia.com/PHPjc/736832.html www.bkjia.com true http://www.bkjia.com/PHPjc/736832.html techarticle with the following 4 code examples, what do you think they are creating objects and getting the speed ordering of member variables? 1: Set member variable to public, assign action to member variable ...

  • 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.