Performance optimization PHP optimizes performance tuning and tuning to optimize performance how to optimize the computer

Source: Internet
Author: User
Tags php language zend
I. Language performance optimization

1. Using the AB tool under Apache for performance testing
Test: ab-n100-c100 https://www.baidu.com/(Request 100 times, concurrency is 100)
Focus on two volumes:
Requests per second (number of requests per second) and time per request (average response times)
2. Try to use PHP built-in variables, constants, functions, Reason: PHP code needs to go through the Zend Engine to scan the Zend to recognize the syntax, and then be parsed into opcode code, and then be executed.
3. Built-in functions also have advantages and disadvantages, to understand the use of high-performance functions, to understand the time complexity of the implementation of functions, isset and array_key_exists two functions as an example.
4. Avoid the magic function of PHP as much as possible.
5. Suppress the use of the error suppressor @, because some code is inserted before and after the statement. Use the tool VLD to view php's opcode.

6. Reasonable use of memory (recommended to release variables with unset)

7. Proper use of regular expressions (due to backtracking)

8. Avoid calculation in the loop, example: for ($i =0; $i < span="">

9. Reduce compute-intensive operations. Reason: Php language features decided, PHP is written in C language, in the upper level, the implementation of the words must pass C to run, just a link effect. PHP is good at connecting webserver with backend services, UI presentation. So intensive business is best achieved through other languages, otherwise it will become a performance bottleneck.

10. Be sure to use quoted strings for key values. Otherwise, PHP will first look for a constant value, if not found will be treated as a string, which requires additional overhead.

Two. Peripheral performance optimization

The main impact performance mainly has the following 5 points:

1.Linux environment;

2. file storage (HDD);

3. Database;

4. Memory Cache (Memcache,redis)

5. Network

Optimization is recommended in the following ways:

1. reduce file class operations, sorted by cost size: Read/write Memory <<><><>< span=""> <><><>

2. Optimize network requests:
Reason: 1. The uncertainty of the interface; 2. Network stability
Optimization method:
2.1. Set timeout time (a connection timeout; b read timeout; c write timeout);
2.2. Parallelization of the serial request;
A) use of curl_multi_* ();
b) Use the swoole extension.

3. compress the PHP interface output:
Using gzip compression
Benefits: Facilitates faster reception of data by client side;
Cons: Extra CPU overhead

4. Cache Duplicate content: multiple requests, the content is not changed under the circumstances;

5.smarty Tuning and time window overlap ideas (which can be used in case the latter task is not strongly dependent on the previous task), which shortens the running time in parallel.

6. Analyze performance using the Xhprof tool;

Three. PHP Performance Bottleneck Solution

Opcode cache:php Extended APC
Extended implementation: Replace the high-frequency business logic in the original PHP code with PHP extensions
Runtime Optimization: HHVM

The above describes the performance optimization of the PHP optimization, including performance optimization, PHP content, I hope to be interested in PHP tutorial friends helpful.

  • 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.