PHP Performance Optimization

Source: Internet
Author: User

 

Directory

1. code optimization: two common "code optimization" methods .... 3

1.1 avoid code redundancy in terms of code quality .... 3

1.2 remove unnecessary code comments... 3

2. Use cache technology... 4

3. Web content compression... 5

4. Database optimization... 5

4.1 database design... 5

4.2 SQL statement optimization... 5

4.3 MySQL cache... 6

1. Code Optimization: two common "code optimization" methods.

One is to improve the code quality, and the other is to optimize the website code through the Optimization engine.

1.1 avoid code redundancy in terms of code quality.

(1) do not select the database in a loop.

(2) InProgramUse require_once () or require () as much as possible in the file inclusion code. Use include_once () or include () in the file in the process control area. Use the full path to include the file, it takes less time to parse the operating system path.

(3) determine the maximum number of cycles before executing the for loop. Do not calculate the maximum number every cycle.

(4) Cancel unused variables at any time, especially large arrays, to release the memory.

(5) The use of branch statements (switch, case) is better than the use of multiple if and elseif statements.

(6) using single quotes instead of multiple quotes to contain strings is faster, because single quotes do not need to parse variables.

(7) Avoid using sentences such as select * From mytable.

1.2 remove unnecessary code comments

The Zend optimizer engine is used to increase the running speed without removing additional comments.

Installing this engine is simple. Download the version of the corresponding platform, uncompress the compressed file, add the following two lines in the PHP. ini file, and restart the web server.

Zend_optimizer.optimization_level = 15

Zend_extension = "/path/to/zendoptimizer. So"

Zend_loader.enable = off

For the Win32 platform:

Zend_optimizer.optimization_level = 15

Zend_extension_ts = "C: \ path \ To \ zendoptimizer. dll"

Zend_loader.enable = off

In fact, the third row is optional. Because it seems that turning zend_loader off can improve the speed, it is worth putting the third line in PHP. ini. Note that you are not using Zend encryption.

2. Use Cache Technology

(1) memcached is the most widely used memory cache system, which puts database data in a large memory pool, reduces database load in dynamic applications, and improves access speed.

(2) Static cache static cache refers to static pages, which directly generate HTML, XML, and other text files, and re-generate them once upon update. It is suitable for pages that do not change much.

3. Web content Compression

Almost all browsers support gzip compression. gzip can reduce the output by 80%, and the cost is about to increase the CPU computing workload by 10%. However, what you earn is that not only does the bandwidth usage decrease, but your page loading will become fast and your PHP site performance is optimized.

You can enable it in PHP. ini.

Zlib. output_compression = on

Zlib. output_compression_level = (level) (level may be a number between 1 and 9. You can set different numbers to make it suitable for your site .)

If you use Apache, you can also activate the mod_gzip module, which is highly customizable.

4. Database optimization 4.1 Database Design

(1) Select the appropriate field type and character Length

(2) index creation

(3) files, images, and other large files are stored in the file system, without the need for Databases

(4) concentrated batch operations are preferred to avoid frequent read/write operations

4.2 SQL statement Optimization

(1) read/write splitting if the database is under great pressure and cannot be supported by one machine, you can use MySQL replication to synchronize multiple machines and distribute the database pressure.

(2) For example, do not use functions on the left side of the operator as much as possible; Use transactions to complete operations that require a series of SQL statements; keep the two field types of operations in the SQL statement unchanged; and so on ......

4.3 MySQL Cache

When querycache is enabled, when a query receives the same query as the previous one, the server will query the cache-type search results instead of re-analyzing and executing the previous query.

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.