Summary of 53 points of experience to improve PHP programming efficiency

  • 2020-03-31 21:04:23
  • OfStack

It is faster to include strings in single quotes instead of double quotes. Because PHP searches for variables in strings surrounded by double quotes, and single quotes don't, note that only echo can do this, which is a "function" that takes multiple strings as arguments.

1. If you can make a method of a class static, make it as static as possible, and it will be nearly four times faster.

2. $row['id'] is 7 times faster than $row['id'].

Echo is faster than print and USES multiple parameters of echo instead of string concatenation, such as echo $str1,$str2.

4. Determine the maximum number of loops before executing the for loop. Instead of calculating the maximum foreach loop, use foreach instead.

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 absolute paths for include files, because it avoids the speed at which PHP can find files in the include_path, and it takes less time to parse operating system paths.

If you want to know when the script starts executing, it is better to use $_SERVER[' REQUEST_TIME'] rather than time().

10. Functions replace regular expressions to accomplish the same function.

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 that accepts the array as an argument for the query and replacement.

13. Using a switch case is better than using multiple if, else if statements.

Blocking error messages with @ is very, very inefficient.

Open apache's mod_deflate module to speed up your web browsing.

16, the database connection when the end of use should be closed, do not use a long connection.

Error messages are expensive.

18. 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 2 times slower than incrementing a local variable.

Increment an object property (e.g. $this-> Prop++) is 3 times slower than incrementing a local variable.

Incrementing an undefined local variable is 9 to 10 times slower than incrementing a predefined local variable.

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

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

Methods in derived classes run faster than the same methods defined in the base class.

25. Calling an empty function with one argument takes the same time as performing a local variable increment operation 7 to 8 times. A similar method call takes approximately 15 local variable increment operations.

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.

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

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

29. You automatically use the strlen() function when you manipulate a string and need to verify that the 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.
(examples below)

If (strlen ($foo) < 5) {echo "Foo is too short" $$}

(compare this 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() 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 ++ will be 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.

It is not necessary to be object-oriented (OOP), object-oriented tends to be expensive, each method and object call will consume a lot of memory.

36. You don't need classes to implement all data structures. Arrays are also useful.

37. Instead of subdividing your methods too much, think carefully about which code you really want to reuse.

You can always break down 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.
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.
Mod_zip can be used as an Apache module to instantly compress your data and reduce the amount of data transferred by 80%.

43. 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;

44, as far as possible less file operation, although PHP file operation efficiency is not low;

45, optimize the Select SQL statement, as far as possible to do less Insert, Update operations (in the Update, I was criticized);

46. Use PHP internal functions whenever possible (but I wasted time writing a custom function to find one that didn't exist in PHP). ;

Don't declare variables inside a loop, especially large variables: objects. ;

48. Multidimensional arrays should not be looped and nested.

49. Don't use regular expressions when you can manipulate functions with PHP's internal strings;

Foreach is more efficient. Use foreach instead of while and for loops.

51. Use single quotes instead of double quotes to refer to strings;

52. "replace I = I +1 with I +1. In line with the habits of c/c++, high efficiency ";

53. For global variables, unset() should be dropped after use;


Related articles: