Home  >  Article  >  Backend Development  >  Everyone needs to know the simple PHP performance optimization points to note, what you need to know about PHP performance optimization_PHP tutorial

Everyone needs to know the simple PHP performance optimization points to note, what you need to know about PHP performance optimization_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:01:31819browse

Everyone needs to know the simple PHP performance optimization points to note. PHP performance optimization

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

<&#63;php
$str="hello world";
 阿
//strlen($str)放在外面
for($i=0;$i<strlen($str);$i++){
 //do something
}
&#63;>

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.

Articles you may be interested in:

  • Optimize PHP efficiency and some methods to improve PHP performance
  • PHP performance optimization analysis tool XDebug large website debugging tool
  • PHP performance optimization produces highly optimized code
  • PHP performance optimization tool chapter Benchmark class debugging execution time
  • LAMP server performance optimization techniques to accelerate PHP
  • 50 PHP program performance optimization Methods
  • php import large amounts of data to mysql performance optimization tips

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1088788.htmlTechArticleEveryone needs to know the simple PHP performance optimization points to pay attention to. You need to know what PHP performance optimization situations are, and you may encounter performance problems: 1 .Inappropriate use of php syntax 2. Use the php language to do what it is not good at...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn