Under what circumstances, you may encounter performance problems:
1. Inappropriate use of php syntax
2. Use the php language to do things it is not good at
3. The service connected using php language is not good
4.php’s own shortcomings
5. Questions that I don’t know either
General situation: PHP performance problems do not exceed half (generally 30%~40%)
Directions for solving PHP performance problems:
PHP language-level performance optimization—>Performance optimization of PHP peripheral issues (connected services, network environment)—>PHP language itself analysis and optimization
(php language level)
Optimization point: write less code and use more of PHP’s own capabilities
Problem: Self-written code has a lot of redundancy and poor readability, resulting in low performance
Why it’s low: PHP code needs to be compiled and parsed into the underlying language. This process will be processed for every request, which is very expensive
Method: Use more PHP built-in variables, constants, and functions
Optimization points: performance advantages and disadvantages of PHP built-in functions
Situation description: PHP built-in functions still have speed differences between them
Suggestion: Learn more about the time complexity of PHP’s built-in functions
Optimization point: use magic functions as little as possible
Situation description: The magic function provided by php has poor performance
Why performance is low: In order to save trouble for PHP programmers, the PHP language has done a lot for you
Good method: avoid using php magic functions as much as possible
Optimization point: error suppressor that generates additional overhead@
Actual logic: Before starting the code, set the highest error reporting level, and then set the error reporting level again after the code is finished. Add Opcode and ignore errors
Optimization point: reasonable use of memory
Situation description: PHP has a memory recycling mechanism to guarantee the bottom line, but please be careful when using memory
Suggestion: Use unset() to release unsuitable memory in a timely manner (note: unset() may not be able to be logged out)
Optimization point: Use regular expressions as little as possible
Situation description: The backtracking overhead of the current expression is relatively large, "don't do ceramic work without diamonds"
Suggestion: Use string processing functions to implement the same logic
Optimization point: avoid doing operations within a loop
Situation description: The calculation formula within the loop will be repeatedly calculated
<?php $str="hello world"; 阿 //strlen($str)放在外面 for($i=0;$i<strlen($str);$i++){ //do something } ?>
Optimization point: reduce computing-intensive business
Situation description: PHP is not suitable for intensive computing scenarios
Why? PHP language characteristics determine that PHP is not suitable for large data calculations
PHP is suitable for scenarios: suitable for connecting Webserver and back-end services, and UI presentation
Optimization point: Be sure to use quoted strings as key values
Situation description: PHP will treat key values without quotes as constants, resulting in the overhead of looking up constants
Suggestion: Strictly use quotes as key values
-------------------------------------------------
(Performance optimization of PHP peripheral issues)-
Running environment, file storage, database, cache, network
Reduce file operations
Overhead order of common PHP scenarios:
Read and write disk, read and write database, read and write memory, read and write network data
Read and write memory<
Optimize network requests
Pitfalls of network requests:
1. Uncertain factors of the other party’s interface
2. Network stability
How to optimize network requests?
1. Set timeout
a) Connection timeout 200ms
b) Read timeout 800ms
c) Write timeout 500ms
2. Parallelize serial requests
a) Use curl_multi_*()
b) Use swoole extension
Compress php interface output
Cache duplicate calculation content
Under what circumstances should the output content be cached?
Multiple requests but the content remains unchanged
The idea of overlapping time windows
Bypass solution
Analyze PHP’s own analysis and optimization:
Test with tools
Solution to PHP performance bottleneck:
Opcode cache (caching in the last step of code compilation) PHP extension APC is used for Opcode caching
Supplementary instructions for using the stress testing software:
Ab -h
apache Benchmark (ab) is a stress testing software provided by Apache. This stress testing software will be included when installing the apache server
Use: ./ab -n1000 -c100 http://www.baidu.com/
-n number of requests -c number of concurrency url target stress test address
The above content introduces you to simple PHP performance optimization points. I hope that sharing this article can help everyone.