What happens, you may experience a performance problem:
Improper use of 1.php syntax
2. Use the PHP language to do something that it's not good at
3. The service that connects with the PHP language does not give the force
4.php of its own short board
5. I do not know the question
General: PHP performance problem does not exceed One-second (general 30%~40%)
PHP Performance Problem Resolution direction:
PHP language-level performance optimization->php peripheral problem performance optimization (connected service, network environment)->php Language analysis and optimization
(PHP language level)
Optimization point: Write less code, more PHP capabilities
Problem: Since write code redundancy more, poor readability, resulting in low performance
Why Low: PHP code needs to be compiled and parsed into the underlying language, and this process is processed every time the request is expensive
Methods: Use PHP built-in variables, constants, functions more
Optimization point: The performance of PHP built-in functions
Situation Description: PHP built-in functions, there are still differences in speed
Recommendation: Learn more about the time complexity of PHP built-in functions
Optimization point: Use magic function as little as possible
Situation Description: PHP provides magic functions, poor performance
Why performance is low: In order to save for PHP programmers, PHP language for you to do a lot
A good way: Avoid using PHP magic functions as much as possible
Optimization point: An error suppressor that generates extra overhead
The actual logic: Before the code start, set the highest error level, after the end, and then reply to set the error level. Add opcode, ignore error
Optimization point: reasonable use of memory
Case Description: PHP has a memory recovery mechanism to the minimum, but also please use memory carefully
Recommendation: Use Unset () to release the memory in time (note: unset () The situation that does not write off)
Optimization point: Use regular expressions as little as possible
Description: The backtracking overhead of the expression is large, "no diamond do not embrace ceramic live"
Recommendation: Using string processing functions to implement the same logic
Optimization point: Avoid doing operations within a loop
Description: The calculation in the loop will be repeated
<?php
$str = "Hello World";
a//strlen ($STR) is placed outside for
($i =0; $i <strlen ($STR); $i + +) {
//do something
}
?>
Optimization point: Reduce compute-intensive business
Scenario Description: PHP is not suitable for intensive operations scenarios
Why? PHP language features determine that PHP is not suitable for large data operations
PHP fit scenario: Suitable for cohesive webserver and back-end services, UI rendering
Optimization point: Be sure to use the quoted string to do the key value
Case Description: PHP will use a key value without quotes as a constant, resulting in the cost of finding constants
Recommendation: Use quotes strictly as key values
--------------------------------------------
(Performance optimization for PHP peripheral problems)-
Operating environment, file storage, database, caching, network
Reduce File class operations
The cost order of common PHP scenarios:
Read and write disk, read and write database, read and write memory, read and write network data
Read/write memory << read-write database < read-write disk < read-write network data
Optimizing Network Requests
Pit of the network request:
1. The interface of the other side of the uncertainties
2. Network stability
How do I optimize network requests?
1. Set timeout time
A) connection Timeout 200ms
b) Read Timeout 800ms
c) Write timeout 500ms
2. Parallelization of Serial requests
A) use of curl_multi_* ()
b) using the Swoole extension
Compress PHP Interface output
Cache Repeat calculation content
Under what circumstances do you cache the output?
Multiple requests, content invariant
Overlapping time window idea
Bypass scheme
Analyze and optimize PHP itself:
Testing with tools
PHP Performance Bottleneck Solution:
Opcode Cache (The last step of Code compilation caching) PHP extension APC do Opcode cache
Additional stress test Software usage Instructions:
Ab-h
Apache Benchmark (AB) is a stress test software provided by Apache that will carry the software when installing the Apache server
Using:./ab-n1000-c100 http://www.baidu.com/
-N Request number-c concurrent Number URL target pressure-measuring address
The above content to introduce a simple PHP performance optimization points of attention, I hope this article to share can help everyone.