Some php programming performance optimization summary

Source: Internet
Author: User
Recently, I am optimizing the performance of the php program. I will record some things I found after testing for later use.

Recently, I am optimizing the performance of the php program. I will record some things I found after testing for later use.

  1. First, you need to track and process slow operations or pages. you can use webGrind to see where the main problem is.
  2. Secondly, for method optimization, one is to view the optimization of the statements called in the method, the other is to optimize the algorithm of the method itself, and the location of the called method is also very important. generally, calling methods in other files is not faster than calling methods on this page. if the method is frequently used and the calling process is time-consuming, for performance consideration, you can port this method to this page.
  3. The statement optimization in the method body includes the optimization of the called method. for example, if you use the statement $ a = '', you should not use $. length () = 0, more optimized is empty ($ a), try to use the best performance method when writing code, 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.
  4. Use foreach instead of for loop as much as possible
  5. Reduce the number of requests to the server as much as possible, which must be compressed from javascript, css compression, and the image should be in png format as much as possible. In addition, it is best to use the image splitting css method to load the image.
  6. Enable full-site code compression and gzip compression
  7. Some php binary code optimization tools, such as xcache, can be used to optimize the background code.
  8. You can consider using some cutting-edge technologies such as memcache to store some small strings, which is much better than file cache performance. you can also consider using redias.
  9. If the program does not reference external files, do not reference them. it also takes some time to reference the files. try to reference as few other files as possible. In addition, for some Branch statements, it is a waste of effort to prevent useless statements from accessing the current branch.
  10. If it cannot be changed, you can consider optimizing the algorithm and framework.

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.