PHP code optimization member variable fetch speed contrast _php skill

Source: Internet
Author: User

There are 4 code examples where do you think they create objects and get the speed ordering of the member variables?

1: Set member variable to public, assign value to member variable by assignment operation, get variable directly

Copy Code code as follows:

<?php
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, and get the variable directly
Copy Code code as follows:

<?php
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, and get the variable by magic method
Copy Code code as follows:

<?php
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 by the constructor, and get the variable by the Member method
<?php
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 speed of execution: 1243
Let's look at the opcode first:
1:

Copy Code code 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 $0, ' id '
8 ECHO $

2:
Copy Code code 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 Code code 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 Code code 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 opcode above, what can we find by referring to the opcode implementation corresponding to the Zend_vm_execute.h file?

The process of creating objects in the PHP kernel is divided into three steps:

Zend_fetch_class gets the variable of the storage class based on the class name, and is actually looking for a hashtalbe EG (class_table)
NEW initializes the object and points ex (call)-&GT;FBC to the constructor pointer.
Call the constructor, and its invocation is the same as other function calls, and is called Zend_do_fcall_common_helper_spec

Second, the Magic method of the call is triggered by conditions, not direct calls, such as our example of the member variable ID acquisition

(Zend_std_read_property), the steps are:
Gets the property of the object, if it exists, takes the second step, and if there are no related attributes, go to step three
From the object's properties to find if there is a property corresponding to the name, 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, the error
Back to the sort of question:

What is the difference between the first and the second?

The second opcode is less than the first, but slower than the first, because the constructor has a lot of parameters and a opcode with a parameter. Parameter processing is a relatively time-consuming operation, when we do code optimization, some unnecessary parameters can be removed; When a function has multiple parameters, it is possible to consider wrapping it through an array and passing it in.

Second, why is the third slowest?

Because it gets a parameter that is essentially a call to an object member method, the invocation cost of the method is higher than the gain of the variable

Third, why the fourth one faster than the third one?

Because the operation of the fourth essentially gets the variable, only its internal implementation of the Magic method calls, in contrast to the user-defined method, the internal function of the call efficiency will be high. So don't reinvent the wheel when we have some methods that the PHP kernel implements can be invoked.
Why is the fourth one slower than the second?
Because in the process of getting a variable in a PHP object, when the member variable is not in the definition of the class, it will invoke the PHP-specific magic method __get, one more time to invoke the Magic method.

To sum up:

1. Using PHP built-in functions
2. Object-oriented (OOP), object-oriented, often expensive, each method and object invocation consumes a lot of memory.
3. Try to use less magic methods-unless necessary, do not use the framework, because the framework has a lot of magic methods to use.
4. In performance-first scenarios, it's a good idea to use member variables when you need OOP.
5. Can use the PHP syntax structure do not use the function, can use the built-in function does not write by oneself, can use the function not to use the object

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.