Home  >  Article  >  php教程  >  PHP basic tutorial discusses some summary of PHP programming performance optimization

PHP basic tutorial discusses some summary of PHP programming performance optimization

巴扎黑
巴扎黑Original
2016-11-07 16:37:031132browse

First of all, you need to track and process some slow-response operations or pages. You can use webGrind to see where the main problems are.

Secondly, for the optimization of methods, one is to check the optimization of the statements called in the method, and the other is to check the optimization of the statements called in the method. For the optimization of the algorithm of the method itself, the location of the called method is also very important. Generally speaking, calling methods in other files is not as fast as calling the method on this page. If it is a frequently used method and the calling process is time-consuming, For performance reasons, this method can be ported to this page.

The optimization of the statements in the method body includes the optimization of the called method. For example, it is better to use the statement $a=='' than to use $a.length()==0. The more optimized one is empty($a). When writing code Try to use the method with the best performance, for example: preg_replace is not as good as str_replace, str_replace is not as good as strtr, use strtr for equal length replacement, use str_replace for unequal length replacement

Try to use foreach instead of for for loops

Try to reduce the number of requests to the server , this depends on javascript compression, css compression, and pictures should be in png format as much as possible. In addition, it is best to use the css method of splitting pictures to load pictures.

 Turn on whole-site code compression and enable gzip compression

You can consider using some PHP binary code optimization tools, such as xcache to optimize the background code

You can consider using some of the most cutting-edge technologies such as: memcache to store some small strings. This performance is much better than file caching. You can also consider using redias and the like.

If you can't reference external files in the program, don't reference them. Referring to files also takes a certain amount of time. Reference other files as little as possible. In addition, for some branch statements, if they are useless to this branch, you must not let this branch Also visited, this is a waste.

If you really can’t change it, you can consider optimizing the algorithm and framework.

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