Ant learning PHP performance optimization (2) PHP language-level performance optimization

Source: Internet
Author: User
Ant Financial learns PHP performance optimization (2) PHP language-level performance optimization (part 1)

3. Optimization: use as few magic functions as possible

Situation description: Magic functions provided by PHP have poor performance.

Why? To save PHP programmers trouble, the PHP language has done a lot

Good method: Avoid using PHP magic functions as much as possible. weigh the advantages and disadvantages when necessary.

4. Optimization point: error blocker with additional overhead @

Situation description: the error blocker provided by PHP is only for the convenience of "lazy"

@ Actual logic of the symbol: added opcode before and after the code starts, ignoring the error.

Good suggestion: Do not use the @ error blocker whenever possible.

5. Optimization: reasonable use of memory

Situation description: PHP has a memory recovery mechanism, but please be careful when using the memory.

Good suggestion: use unset () to release unused memory. (Note: unset logs cannot be canceled)

6. Optimization: use regular expressions as few as possible

Situation description: Regular expressions have a high overhead for backtracking. if regular expressions have poor foundation, they should be used with caution.

Good suggestion: use string processing functions to implement the same logic.

7. Optimization: avoid computation in the loop

Situation description: the computation formula in the loop will be repeated.

Sample code:

// Incorrect practice. calculate the length of $ str for each loop $ str = 'Hello World'; for ($ I = 0; $ I
   
  

8. Optimization: reduce computing-intensive business

Case description: PHP is not suitable for intensive computing scenarios. PHP features make PHP not suitable for big data computing.

Applicable scenarios of PHP, suitable for connecting webserver and backend services and UI presentation.

9. Optimization: Always use a string with quotation marks as the key value.

Case description: PHP treats key values without quotation marks as constants, resulting in overhead for constant search.

Good suggestion: strictly use the quotation mark key value.

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.