PHP basic tutorial optimization summary

巴扎黑
Release: 2023-03-03 11:34:01
Original
951 people have browsed it

The editor is currently optimizing the performance of PHP programs. Some things discovered after testing are recorded for future use.

  First of all, you need to track and deal with 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 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 methods on this page. If it is a frequently used method and the calling process is more time-consuming, , for performance reasons, this method can be transplanted to this page.

  The optimization of the statements in the method body includes the optimization of the calling 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 the 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.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template