1. If a method can be statically declared. The rate can be increased to 4 times.
Faster than print.
3. Use echo's multiple parameters (translator's note: refers to using commas instead of periods) instead of string concatenation.
4. Determine the maximum number of loops before executing the for loop, and do not calculate the maximum value every time.
5. Log out those unused variables, especially large arrays, to free up memory.
6. Try to avoid using __get, __set, __autoload.
7.require_once() is expensive.
8. Use the full path when including files, and it takes less time to parse the operating system path.
9. If you want to know the time when the script starts executing (translation note: that is, the server receives the client request), it is better to use $_SERVER['REQUEST_TIME'] than time().
10. Functions perform the same function instead of regular expressions.
11. The str_replace function is faster than the preg_replace function, but the strtr function is four times more efficient than the str_replace function.
12. If a string replacement function can accept arrays or characters as parameters, and the parameters are not too long, then you can consider writing an additional piece of replacement code so that each parameter is passed is one character, rather than just writing one line of code to accept arrays as parameters for query and replacement.
13. Using a select branch statement (translation note: that is, switch case) is better than using multiple if and else if statements.
14. The practice of using @ to block error messages is very inefficient.
15. Open the mod_deflate module of apache.
16. The database connection should be closed when it is used.
17.$row['id'] is 7 times more efficient than $row[id].
18. Error messages are expensive.
19. Try not to use functions in for loops, such as for ($x=0; $x < count($array); $x) will call the count() function every time the loop.
20. Increasing local variables in the method is the fastest. Almost comparable to calling local variables in a function.
21. Incrementing a global variable is twice as slow as incrementing a local variable.
22. Incrementing an object property (such as: $this->prop++) is 3 times slower than incrementing a local variable.
23. Incrementing an unpredefined local variable is 9 to 10 times slower than incrementing a predefined local variable.
24. Defining only one local variable without calling it in the function will also slow down (its degree is equivalent to incrementing a local variable). PHP will probably check whether there are global variables.
25. The method call seems to have nothing to do with the number of methods defined in the class, because I added 10 methods (before and after the test method) but no changes in performance.
26. Methods in derived classes run faster than the same methods defined in the base class.
27. Calling an empty function with one parameter takes the same time as performing 7 to 8 local variable increment operations. Similar method calls take nearly 15 local variable increment operations.
28. Use single quotes instead of double quotes to include strings, which will be faster. Because PHP searches for variables in strings surrounded by double quotes, single quotes will not. Of course, this is only possible if you don't need to include variables in a string.
29. When outputting multiple strings, use commas instead of periods to separate the strings, which is faster. Note: Only echo can do this. It is a "function" that can treat multiple strings as parameters (translator's note: The PHP manual says echo is a language structure, not a real function, so the function is added with double quotes).
It takes 2 to 10 times slower to parse a PHP script than to parse a static HTML page. Try to use more static HTML pages and less scripts.
31. Unless the script can be cached, it will be recompiled every time it is called. Introducing a PHP caching mechanism can usually improve performance by 25% to 100% to avoid compilation overhead.
32. Try to make caches, you can use memcached. memcached is a high-performance memory object caching system that can be used to accelerate dynamic web applications and reduce database load. It is useful for caching of operation codes (OP codes), so that scripts do not have to recompile each request.
33. When operating a string and need to check whether its length meets certain requirements, you will use the strlen() function for granted. This function is quite fast to execute because it does not do any calculations and only returns the known string length stored in the zval structure (the built-in data structure of C, used to store PHP variables). However, since strlen() is a function, it will be a bit slower, because function calls will go through many steps, such as lowercase letters (translation note: refers to lowercase function names, PHP does not distinguish between lowercase function names), hash search, and will be executed with the called function. In some cases, you can use the isset() technique to speed up execution of your code.
(Sample the following)
if (strlen($foo) < 5) { echo "Foo is too short"$$ }
(Compare with the following tips)
if (!isset($foo{5})) { echo "Foo is too short"$$ }
Calling isset() happens to be faster than strlen(), because unlike the latter, isset() as a language structure means that its execution does not require function lookup and letter lowercase. That is, you don't actually spend much money in the top-level code that checks the length of the string.
34. When the increment or decrement of the variable $i is performed, $i++ will be slower than ++$i. This difference is unique to PHP and does not work for other languages, so please do not modify your C or Java code and expect them to get faster immediately, which is useless. ++$i is faster because it only requires 3 instructions (opcodes), while $i++ requires 4 instructions. Post-increment actually produces a temporary variable that is then incremented. The pre-increment directly increments on the original value. This is a type of optimization processing, as Zend's PHP optimizer does. Keeping this optimization process in mind is a good idea, because not all instruction optimizers do the same, and there are a large number of Internet service providers (ISPs) and servers that do not have instruction optimizers assembled.
35. It is not something that must be object-oriented (OOP). Object-oriented is often very expensive, and each method and object call consumes a lot of memory.
36. It is not necessary to use classes to implement all data structures, arrays are also useful.
37. Don’t subdivide the methods too much. Think carefully about what codes are you really planning to reuse?
38. When you need it, you can always break the code into methods.
39. Try to use a large number of built-in PHP functions.
40. If there are a lot of time-consuming functions in the code, you can consider implementing them in C extension.
41. Evaluate and verify your code. The verifier will tell you which parts of the code consume how much time. The Xdebug debugger contains a verification program that evaluates the verification to show the bottleneck of the code in general.
42.mod_zip can be used as an Apache module to compress your data instantly and reduce the data transmission by 80%.
Faster than print.
3. Use echo's multiple parameters (translator's note: refers to using commas instead of periods) instead of string concatenation.
4. Determine the maximum number of loops before executing the for loop, and do not calculate the maximum value every time.
5. Log out those unused variables, especially large arrays, to free up memory.
6. Try to avoid using __get, __set, __autoload.
7.require_once() is expensive.
8. Use the full path when including files, and it takes less time to parse the operating system path.
9. If you want to know the time when the script starts executing (translation note: that is, the server receives the client request), it is better to use $_SERVER['REQUEST_TIME'] than time().
10. Functions perform the same function instead of regular expressions.
11. The str_replace function is faster than the preg_replace function, but the strtr function is four times more efficient than the str_replace function.
12. If a string replacement function can accept arrays or characters as parameters, and the parameters are not too long, then you can consider writing an additional piece of replacement code so that each parameter is passed is one character, rather than just writing one line of code to accept arrays as parameters for query and replacement.
13. Using a select branch statement (translation note: that is, switch case) is better than using multiple if and else if statements.
14. The practice of using @ to block error messages is very inefficient.
15. Open the mod_deflate module of apache.
16. The database connection should be closed when it is used.
17.$row['id'] is 7 times more efficient than $row[id].
18. Error messages are expensive.
19. Try not to use functions in for loops, such as for ($x=0; $x < count($array); $x) will call the count() function every time the loop.
20. Increasing local variables in the method is the fastest. Almost comparable to calling local variables in a function.
21. Incrementing a global variable is twice as slow as incrementing a local variable.
22. Incrementing an object property (such as: $this->prop++) is 3 times slower than incrementing a local variable.
23. Incrementing an unpredefined local variable is 9 to 10 times slower than incrementing a predefined local variable.
24. Defining only one local variable without calling it in the function will also slow down (its degree is equivalent to incrementing a local variable). PHP will probably check whether there are global variables.
25. The method call seems to have nothing to do with the number of methods defined in the class, because I added 10 methods (before and after the test method) but no changes in performance.
26. Methods in derived classes run faster than the same methods defined in the base class.
27. Calling an empty function with one parameter takes the same time as performing 7 to 8 local variable increment operations. Similar method calls take nearly 15 local variable increment operations.
28. Use single quotes instead of double quotes to include strings, which will be faster. Because PHP searches for variables in strings surrounded by double quotes, single quotes will not. Of course, this is only possible if you don't need to include variables in a string.
29. When outputting multiple strings, use commas instead of periods to separate the strings, which is faster. Note: Only echo can do this. It is a "function" that can treat multiple strings as parameters (translator's note: The PHP manual says echo is a language structure, not a real function, so the function is added with double quotes).
It takes 2 to 10 times slower to parse a PHP script than to parse a static HTML page. Try to use more static HTML pages and less scripts.
31. Unless the script can be cached, it will be recompiled every time it is called. Introducing a PHP caching mechanism can usually improve performance by 25% to 100% to avoid compilation overhead.
32. Try to make caches, you can use memcached. memcached is a high-performance memory object caching system that can be used to accelerate dynamic web applications and reduce database load. It is useful for caching of operation codes (OP codes), so that scripts do not have to recompile each request.
33. When operating a string and need to check whether its length meets certain requirements, you will use the strlen() function for granted. This function is quite fast to execute because it does not do any calculations and only returns the known string length stored in the zval structure (the built-in data structure of C, used to store PHP variables). However, since strlen() is a function, it will be a bit slower, because function calls will go through many steps, such as lowercase letters (translation note: refers to lowercase function names, PHP does not distinguish between lowercase function names), hash search, and will be executed with the called function. In some cases, you can use the isset() technique to speed up execution of your code.
(Sample the following)
if (strlen($foo) < 5) { echo "Foo is too short"$$ }
(Compare with the following tips)
if (!isset($foo{5})) { echo "Foo is too short"$$ }
Calling isset() happens to be faster than strlen(), because unlike the latter, isset() as a language structure means that its execution does not require function lookup and letter lowercase. That is, you don't actually spend much money in the top-level code that checks the length of the string.
34. When the increment or decrement of the variable $i is performed, $i++ will be slower than ++$i. This difference is unique to PHP and does not work for other languages, so please do not modify your C or Java code and expect them to get faster immediately, which is useless. ++$i is faster because it only requires 3 instructions (opcodes), while $i++ requires 4 instructions. Post-increment actually produces a temporary variable that is then incremented. The pre-increment directly increments on the original value. This is a type of optimization processing, as Zend's PHP optimizer does. Keeping this optimization process in mind is a good idea, because not all instruction optimizers do the same, and there are a large number of Internet service providers (ISPs) and servers that do not have instruction optimizers assembled.
35. It is not something that must be object-oriented (OOP). Object-oriented is often very expensive, and each method and object call consumes a lot of memory.
36. It is not necessary to use classes to implement all data structures, arrays are also useful.
37. Don’t subdivide the methods too much. Think carefully about what codes are you really planning to reuse?
38. When you need it, you can always break the code into methods.
39. Try to use a large number of built-in PHP functions.
40. If there are a lot of time-consuming functions in the code, you can consider implementing them in C extension.
41. Evaluate and verify your code. The verifier will tell you which parts of the code consume how much time. The Xdebug debugger contains a verification program that evaluates the verification to show the bottleneck of the code in general.
42.mod_zip can be used as an Apache module to compress your data instantly and reduce the data transmission by 80%.