Small ant learning php performance Optimization (2)--php language-level performance optimization

Source: Internet
Author: User

Pick up the article

3. Optimize points: Use as few magical functions as possible

Scenario Description: PHP provides a magic function that is poorly performing

Why is it? In order to save PHP programmers a lot of trouble, the PHP language to do many

Good approach: Avoid using PHP magic functions as much as possible, and weigh the pros and cons when you need to use them.

4. Optimization point: Error suppressor that generates additional overhead

Case Description: PHP provides error suppressors just for the convenience of "lazy people"

The actual logic of the @ symbol: Before the code starts, after the end, add opcode, ignoring the error.

Good advice: Try not to use the @ error suppressor

5. Optimize points: use memory reasonably

Case Description: PHP has a guaranteed memory recovery mechanism, but please also use memory carefully.

Good advice: Use unset () even if you are releasing unused memory. (Note: unset will not be logged out of the situation)

6. Optimize points: Use regular expressions as little as possible

Case Description: The retrospective overhead of regular expressions is large, and if the basis of regular expressions is not good, use caution.

Good advice: Use string processing functions to implement the same logic.

7. Optimize points: Avoid doing operations within the loop

Description: The calculation in the loop will be repeated.

code example:

Wrong way, each cycle to calculate the length of $str $str = "Hello World"; for ($i =0; $i <strlen ($STR); $i + +) {//Here is the logical}//correct practice, only one length can be calculated $str = ' Hello world ', $len = strlen ($STR); for ($i =0; $i < $len; $i + +) {//This is logic}

8. Optimize points: Reduce compute-intensive business

Scenario Description: PHP is not suitable for intensive computing scenarios. PHP features determine that PHP is not suitable for large data-volume operations.

PHP application scenario, suitable for connecting webserver and backend services, UI rendering.

9. Optimize points: Be sure to use quoted strings as key values

Case Description: PHP will handle unquoted key values as constants, resulting in the overhead of finding constants.

Good advice: Use quote key values strictly.


Small ant learning php performance Optimization (2)--php language-level performance optimization

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.