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 I don’t know either
General situation: PHP performance problems do not exceed half (generally 30%~40%)
Directions for solving PHP performance problems:
phpLanguage-level performance optimization—>phpPerformance optimization of surrounding issues (connected services, network environment)—>phpLanguage self-analysis and optimization
(php language level)
Optimization points: write less code and use more PHPPersonal abilities
Problem: Self-written code has a lot of redundancy and poor readability, resulting in low performance
Why it is low: PHP code needs to be compiled and parsed into the underlying language. This process will be processed for each request, which is very expensive
Method: Use more PHP built-in variables, constants, and functions
Optimization points: phpPerformance pros and cons of 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 the performance is low: In order to save PHP programmers trouble, 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
Recommendation: Use unset() to release unsuitable memory in time (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
$str="hello world";
//strlen($str) is placed outside
for($i=0;$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 Recommendation: 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, 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