Notes for simple php performance optimization

Source: Internet
Author: User
This article will introduce the possible performance problems, the solution direction and optimization points of php performance problems. if you are interested in php performance optimization, learn it together. What are the possible performance problems:

1. incorrect php syntax
2. I used php to do what I was not good.
3. the services connected in php language are not powerful.
4. php's own short board
5. I don't know either.

General Situation: php performance problems cannot exceed 1/2 (generally 30% ~ 40%)

Php performance problem solution:

Php language-level performance optimization-> php peripheral problems performance optimization (connection service, network environment)-> php language self-analysis and optimization

(Php language level)

Optimization: less code writing and more php capabilities

Problem: Self-writing code is redundant and not readable, resulting in low performance.
Why is it low: php code needs to be compiled and parsed to the underlying language. this process is processed every time a request is sent, with a high overhead.
Methods: use php built-in variables, constants, and functions.

Optimization: php built-in functions

Situation description: php built-in functions, there are still slow differences between them
Suggestion: Learn more about the time complexity of php built-in functions

Optimization: use as few magic functions as possible

Situation description: Magic functions provided by php have poor performance.
Why is the performance low: the php language has done a lot for you to save the trouble for php programmers
Good method: Avoid using php magic functions as much as possible

Optimization point: error blocker with additional overhead @

Actual logic: before the code starts, set the highest error level, and then reply to the set error level. Added Opcode to ignore error reporting

Optimization: reasonable memory usage

Situation description: php has a memory recovery mechanism, but please be careful when using the memory
Suggestion: use unset () to release unsuitable memory in a timely manner (note: unset () cannot be canceled)

Optimization: use regular expressions as few as possible

Situation description: the backtracking overhead of the expression is large. "no diamond is used to capture ceramics"
Suggestion: use string processing functions to implement the same logic

Optimization: avoid computation in a loop

Case description: the computation formula in the loop will be repeated.

<? Php $ str = "hello world"; a // strlen ($ str) put it outside for ($ I = 0; $ I
 

Optimization: reduce computing-intensive business

Case description: php is not suitable for intensive computing scenarios.

Why? Php language features make php not suitable for big data computing
Php: suitable for connecting Webserver to backend services and UI presentation

Optimization point: Be sure to 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.
Suggestion: use quotation marks strictly as the key value.

--------------------------------------------

(Performance Optimization for php peripheral problems )-

Runtime Environment, file storage, database, cache, network

Reduce file operations

Common php overhead sequence:
Read/Write disk, read/write database, read/write memory, read/write network data
Read/write memory < <读写数据库<读写磁盘<读写网络数据

Optimize network requests

Network request pitfalls:

1. uncertainty of the peer interface

2. network stability

How to optimize network requests?

1. set the timeout time

A) connection timeout: 200 ms
B) read timeout 800 ms
C) write timeout 500 ms

2. parallelization of serial requests

A) use curl_multi _*()
B) use swoole extension

Compressing php interface output

Cache repeated computing content

Under what circumstances will the output content be cached?

Multiple requests, unchanged content

Overlapping time window ideas

Bypass plan

Analysis of php analysis and optimization:

Test with tools

PHP performance bottleneck solution:
Opcode cache (the last step of code compilation for caching) PHP extension APC for Opcode caching

Supplemental stress testing software instructions:

AB-h

Apache Benchmark (AB) is a stress testing software provided by Apache. this software is provided when the apache server is installed.

Use:./AB-n1000-c100 http://www.baidu.com/

-N requests-c concurrency url target pressure test address

The above content introduces you to some simple considerations for php performance optimization. I hope this article will help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.