Summary of some strategies for PHP program efficiency optimization

  • 2020-03-31 20:57:17
  • OfStack

1. If you can use file_get_contents instead of file, fopen, feof, fgets, etc., try to use file_get_contents because it's much more efficient! But be aware of the PHP version of file_get_contents when opening a URL file;
2. As little file operation as possible, although the file operation efficiency of PHP is not low;
3. Optimize the Select SQL statement, and conduct Insert and Update operations as little as possible (in the Update, I was criticized);
4. Use PHP internal functions whenever possible (but I wasted time writing a custom function to find one that doesn't exist in PHP). ;
Don't declare variables inside the loop, especially large variables: objects. ;
6. Multidimensional arrays should not be nested in loops as much as possible;
7. Don't use regular expressions when you can manipulate functions with PHP's internal strings;
8. Foreach is more efficient. Use foreach instead of while and for loops.
Use single quotes instead of double quotes to refer to strings;
10. "replace I = I 1 with I =1. Compliance with the c/c habit and high efficiency ";
11. For global variables, you should run out of unset()
12. In multiple nested loops, if possible, the longest loop should be placed in the inner layer and the shortest loop in the outer layer to reduce the number of CPU cycles across the cut loop layer and optimize program performance.
40 tips for optimizing your PHP code
1. If a method is static, declare it static. The rate can be increased to 4 times.
Echo is faster than print.
Use echo's multiple arguments instead of string concatenation.
4. Determine the maximum number of loops before executing the for loop; do not calculate the maximum for each loop.
5. Log out variables you don't use, especially large arrays, to free up memory.
6. Try to avoid using s/s, s/s and s/s.
7. Require_once () is expensive.
8. Use the full path when the file is included, and it will take less time to parse the operating system path.
If you want to know when the script starts executing, it is better to use $_SERVER[' REQUEST_TIME'] rather than time().
10. Check to see if you can use strncasecmp, STRPBRK, and stripos functions instead of regular expressions to do the same thing.
The str_replace function is faster than the preg_replace function, but the STRTR function is four times as efficient.
12. If a string replacement function accepts an array or a character as an argument and the argument length is not too long, consider writing an additional replacement code so that the argument is passed one character at a time, rather than just one line of code accepting the array as an argument for the query and replacement.
13. Using a select branch statement is better than using multiple if, else if statements.
Blocking error messages with @ is very inefficient.
15. Open apache's mod_deflate module.
16. Database connections should be closed when finished.
17. $row[' id'] is 7 times more efficient than $row[' id'].
Error messages are expensive.
19. Try not to use functions in the for loop, such as for ($x=0; $x < Count ($array); $x) calls the count() function every time through the loop.
20. The fastest way to increment a local variable is in a method. It's almost as fast as calling a local variable in a function.
Incrementing a global variable is twice as slow as incrementing a local variable.
Increments an object property (e.g. $this-> Prop++) is 3 times slower than incrementing a local variable.
23. Incrementing an undefined local variable is 9 to 10 times slower than incrementing a predefined local variable.
Defining a local variable without calling it in a function also slows things down (by the same amount as incrementing a local variable). PHP will probably check for global variables.
25. Method invocations seem to be independent of the number of methods defined in the class, because I added 10 methods (both before and after the test method), but there was no change 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 argument takes the same time as performing 7 to 8 local variable increments. A similar method call takes approximately 15 local variable increment operations.
28. It is faster to include strings in single quotes instead of double quotes. Because PHP searches for variables in strings surrounded by double quotes, single quotes do not. Of course, you can only do this if you don't need to include variables in the string.
When multiple strings are output, it is faster to separate them with commas instead of periods. Note: only echo can do this, and it's a "function" that takes multiple strings as arguments.
Apache takes 2 to 10 times longer to parse a PHP script than a static HTML page. Use more static HTML pages and less scripting.
31. Unless the script can be cached, it is recompiled every time it is invoked. Introducing a PHP caching mechanism typically increases performance by 25 to 100 percent to eliminate compilation overhead.
32. Cache as much as possible, using memcached. Memcached is a high-performance in-memory object caching system that can be used to speed up dynamic Web applications and reduce database load. A cache of OP code is useful so that the script does not have to be recompiled for each request.
33. You automatically use the strlen() function when you are manipulating a string and need to verify that its length meets a certain requirement. This function performs fairly quickly because it does no calculations and returns only the known string length stored in the zval structure (the built-in data structure of C for storing PHP variables). However, because strlen() is a function, it is somewhat slow because the function call goes through a number of steps, such as lowercase, hash lookup, and so on, along with the called function. In some cases, you can use the isset() technique to speed up the execution of your code.
Ex. (for example)
If (strlen ($foo) < 5) {echo "Foo is too short"; }
Vs. (compare these tips)
If (! Isset ($foo{5})) {echo "foo is too short"; }
Calling isset() happens to be faster than strlen() because, unlike the latter, isset() is a language construct, meaning that its execution does not require function lookups and lowercase letters. That is, you don't actually spend much overhead in the top-level code that verifies the length of the string.
34. When the variable $I is incremented or decrement, $I ++ is slower than ++$I. This difference is PHP specific and doesn't apply to other languages, so don't change your C or Java code and expect it to be instantly faster, it doesn't work. ++$I is faster because it only requires three opcodes and $I ++ requires four. Postincrement actually produces a temporary variable that is then incremented. Preincrement increases directly on the original value. This is a type of optimization, as Zend's PHP optimizer does. It's a good idea to keep this optimization in mind because not all instruction optimizers do the same optimization, and there are plenty of Internet service providers (ISPs) and servers that don't have an instruction optimizer assembled.
35. Not everything has to be object-oriented (OOP), which tends to be expensive, with each method and object call consuming a lot of memory.
36. Not all data structures are implemented with classes, and arrays are useful.
37. Instead of subdividing your methods too much, think carefully about which code you really want to reuse.
38. You can always break code into methods when you need to.
39. Use as many built-in PHP functions as possible.
40. If you have a lot of time-consuming functions in your code, consider implementing them as C extensions.
41. Profile your code. The validator will tell you which parts of the code are consuming how much time. The Xdebug debugger includes a validator, and evaluation validation generally shows bottlenecks in the code.
42.mod_zip can be used as an Apache module to instantly compress your data and reduce the amount of data transferred by 80%.

Related articles: