PHP Performance Optimization Brief

Source: Internet
Author: User
Thanks for MOOC. Com first. Here is a brief introduction of the theory
* *
*PHP performance optimization
*
*1. Problem analysis
*Incorrect use of PHP syntax
*What PHP is not good at
* PHP services are not suck up.
*PHP's own short board
*Other reasons
*2. PHP project performance issues account for the whole project performance issues
*PHP performance problems will not exceed the general performance problems of the whole project, usually accounting for 30% to 40%,
*Optimizing the whole project not only optimizes PHP performance, but also only takes a part of PHP performance problems in the whole project.
*Optimization process:
*1. PHP language level performance optimization; quick to see benefits
*2. Performance optimization of PHP peripheral problems; Web server, mysql, etc
*3. PHP analysis and optimization;
*
*Stress test software
*Installing Apache under Linux will bring ab pressure test tool
*  ./ab -n1000 -c100 http://www.baidu.com
*- N total request - C concurrent
*Note that if there is a white list, you need to add the local IP to the white list first, otherwise it will be considered a DDoS attack;
*Mainly check the number of requests accepted by request per second, the more the better
*Time per request
*
* /
/ *
*
*PHP language level optimization
*1. Write less code and use more PHP's own capabilities;
*For example, PHP built-in functions / variables / constants are used to realize functions;
*2. The performance of PHP built-in functions is good or bad. PHP built-in functions may achieve the same functions, but there are differences;
*For example, isset ($arr [$I]) / array_key_exists ($I, $ARR);
*3. Try to use less magic functions. The magic functions provided by PHP have poor performance;
*For example, time PHP test.php;
*4. Error suppressor with extra overhead;
*Error suppressor is to add opcode before and after the code. The function of opcode is to ignore error reporting. Try not to use it. Try throw better;
*5. Reasonable use of memory;
*Use unset to release unused memory in time;
*6. Try to use less regular expressions;
*Using string processing function is better;
*7. Avoid doing operations in the loop;
*Put the calculation outside the loop and compare the two variables on the loop;
*8. Reduce computing intensive business;
*Large batch of data processing, large batch of log analysis, PHP language written in C, PHP computing costs more than C;
*PHP uses web server and back-end services, UI rendering, string text processing;
*9. Make sure to use quoted string as key value of array;
*
*
*
*The VLD tool can view opcode, which is a PHP extension;
*
*PHP code running process: PHP code is scanned line by line by Zend engine, explaining the syntax that Zend engine can recognize,
*Through the analysis, the opcodes are parsed into opcodes, which are executed to complete the output;
*PHP caches are all opcodes.
* /
* *
*Optimization of PHP peripheral problems
*1. Linux operating environment
*Linux server runs PHP, so the environment is very important, and the fast and slow running of Linux is very important!
*2. File storage, performance of hard disk, writing and reading of hard disk;
*3. MySQL database, based on file system;
*Disk is hardware and database is software;
*The performance of database also determines the speed of PHP;
*4. Cache, memory, hardware memory, software Memcache, redis, etc;
* 5. network
*
*It takes time to connect external database, network + database, PHP file and peripheral service!
*If it's a database problem, it's useless to optimize the syntax!
*
* recommendations:
*1. Reduce file operations; 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;
*Common databases use memory as cache! First, you can only write it to memory, and then you can operate it. It's faster than directly operating disk!
*Network data through socket, is also a disk operation! There is network delay! Can't confirm that the computer room network is excellent! Try to reduce the situation of reading and writing network!
*Database, disk, network are based on file system, based on hard disk!
*Memory operation is based on memory, which directly interacts with memory, with small overhead and faster speed!
*Try to operate more memory database and less disk network data!
*
*Optimization of PHP request network interface:
*Uncertainties / network instability of the opposite interface
*1. Set timeout: connection timeout / read timeout / write timeout. It is recommended that the connection timeout be 200ms, read timeout be 800ms, no more than 1s, write timeout be 500ms!
*2. Parallelizing serial requests: changing curl to curl_multi* () / using the swoole extension
*
*PHP interface, compression PHP interface output!
*Just use gzip! The client side gets data faster, but it will generate additional CPU overhead; when the data is less than 100k, compression has no great effect!
*
*Cache recalculations:
*Multiple requests, content unchanged, using cache; often used in template output, Smarty uses cache function;
*
*Overlapping time window idea: the latter task does not rely on the former one; timing cache!
*Bypass scheme: template rendering link!
*
*
* /
* *
*Specific analysis of PHP performance
*Xhporf tools;
*PHP -- RI xhporf to see if xhporf is supported
* /
* *
*PHP performance bottleneck solution:
*Opcode cache: PHP extends APC; cache opcode! PHP PECL site, see PHP extension!
*Replace the medium and high frequency logic of ah source code with PHP extension
*Runtime optimization: hhvm
* /
* *
* *
*PHP is mainly in reading and writing memory, reading and writing database, reading and writing disk, reading and writing network data;
*The optimization of PHP project is cache optimization / network optimization / database optimization, as well as the deployment of PHP on Linux environment, Linux also has an impact on PHP performance!
*MySQL optimization is very important, other optimization can not be ignored!
*Read and write memory is faster than database, database is faster than disk, disk is faster than network data, because network data is also based on file system, and related to the stability of the network;
*Usually try to avoid getting network data. When you have to use the data provided by the network interface, you should set connection timeout, read timeout, write timeout, etc!
*Then there are multiple operations cache and fewer disk operations. You can use Memcache and redis cache to reduce the cost of reading and writing disks!
*Although the database is also based on the file system, it is faster than the direct operation of the disk, because most of the databases use memory as the cache, and the result will be returned if the cache is written successfully, so it is faster than the direct operation of the disk!
*In a word, it is more memory database, less disk and network data!
*In addition, the performance of Linux system and web server also affects the performance of PHP!
*Then, let's talk about database optimization. MySQL database is commonly used, so it's MySQL optimization!
*
* /
PHP performance optimization

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.