Program Optimization (PHP)

Source: Internet
Author: User
PHP program optimization. png

There are small details that are often overlooked. Sometimes it is often said that optimizing code optimization code, but the actual operation, the most easily overlooked is as follows:

  1. echo is faster than print.

  2. Use Echo's multiple arguments instead of string joins.

  3. To determine the maximum number of loops before executing a for loop, do not calculate the maximum value once per loop, preferably using foreach instead.

  4. For the global variable, it should be unset () off.

  5. Using single quotes instead of double quotation marks to contain strings can be faster. Because PHP searches for a variable in a string surrounded by double quotes, the single quotation mark does not.

  6. function to perform the same function instead of a regular expression.

  7. When the variable $i is incremented or decremented, $i + + will be slower $i than the + +. This difference is unique to PHP, not for other languages, + + $i faster because it requires only 3 instructions (opcodes), $i + + requires 4 instructions. The post increment actually produces a temporary variable, which is then incremented. The predecessor increment is incremented directly on the original value.

  8. Using the Select Branch statement (switch case) is better than using multiple if,else if statements.

  9. Debug PHP code with Var_dump. If you are looking for PHP debugging techniques, I have to say that var_dump should be the target you are looking for, and that this command will meet all your needs in terms of displaying PHP information, and most of the debugging code is related to getting the values in PHP.

  10. The full path is used when the file is included, and less time is required to resolve the operating system path.

  11. It's a bad idea to create global values at will, but sometimes it does. Using global values for database tables or database connection information is a good idea, but do not use global values frequently in your PHP code. In addition, a better approach is to put your global variables in a config.php file.

  12. If you want to know when the script starts executing, use $_server[' request_time '] better than time ().

  13. Open the Apache mod_deflate module.

  14. Using @ To mask error messages is very inefficient.

  15. Try to use a lot of PHP built-in functions.

  16. Incrementing a non-predefined local variable is 9 to 10 times times slower than incrementing one of the pre-defined local variables.

  17. A method in a derived class runs faster than the same method that is defined in the base class.

  18. Defining only a local variable without calling it in the function also slows down the speed (which is equivalent to incrementing a local variable)

  19. Apache Parsing a PHP script is 2 to 10 times times slower than parsing a static HTML page. Use static HTML pages as much as possible and use fewer scripts.

  20. As mentioned earlier, 99% of the most important parts of any PHP Web site may be databases. Therefore, you need to be very familiar with how to use SQL correctly, learn related tables and more advanced database technologies.

  21. Calling an empty function with one parameter takes a time equivalent to performing a local variable increment operation of 7 to 8 times.

  22. When you manipulate a string and need to verify that its length satisfies a certain requirement, you will of course use the strlen () function. This function executes quite quickly because it does not do any calculations and only returns the length of the known string stored in the Zval structure (the built-in data structure of C, used to store PHP variables).

  23. Not all cases must use object-oriented development, object-oriented tends to be expensive, and each method and object invocation consumes a lot of memory.

  24. Unless the script can be cached, it will be recompiled every time it is called. Introducing a set of PHP caching mechanisms can typically improve performance by 25% to 100% to eliminate compilation overhead.

Finally, let's remind you of the things you should be aware of when optimizing your code:

    1. Short code is not equal to fast code many people want to write the code the more concise the better, but the shorter the code sometimes takes longer execution time.

    2. In writing the program should pay more attention to the extension of the program, rather than the pursuit of speed.

    3. Before you optimize your code, look at the parts of the database, because most applications have bottlenecks in the database rather than the code.

    4. Micro-optimization is not worth what is called micro-optimization? As mentioned earlier, the code for the regular Expression section is replaced with a string function instead. This has the following disadvantages: (1) Taking a long time (2) does not fundamentally solve the performance problem (3) It is likely to break the previous code and generate an unknown error (4) pay more than the return

      There is also a misconception that some people, in order to make the program more optimized, consider the optimization in the analysis of the business logic, in order to get better code and change the business logic. This is a very foolish idea, because the purpose of the procedure is to deal with the problems encountered in reality and we cannot get the cart before the horse.

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