Discuss some PHP programming performance optimization summary

高洛峰
Release: 2016-11-29 09:10:48
Original
871 people have browsed it

I have been optimizing the performance of PHP programs recently. I will record some things I discovered after testing 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 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 to minimize 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.

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

Related labels:
php
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!