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.