Methods for optimizing performance of 50 PHP programs

  • 2021-06-28 11:41:42
  • OfStack

1. Use single quotation marks instead of double quotation marks to include strings, which is a little faster.Because PHP searches for variables in a string surrounded by double quotation marks, single quotation marks do not. Note: Only echo can do this; it is a "function" that takes multiple strings as parameters (Note: echo is a language structure, not a real function, so double quotation marks are added to the function).

2. If you can define a class's method as static, try to define it as static, which will increase nearly four times faster.

3. $row ['id'] is seven times faster than $row [id].

4. echo is faster than print and uses multiple parameters of echo (comma instead of period) instead of string connections, such as echo $str1, $str2.

5. Determine the maximum number of cycles before the for cycle is executed. Do not calculate the maximum value once per cycle. It is better to use foreach instead.

6. Unuse unused variables, especially large arrays, to free up memory.

7. Avoid using_u whenever possibleget, uset, uautoload.

8. require_once() is expensive.

9. Use absolute paths whenever possible for include files, as it prevents PHP from removing include_Finding files in path takes less time to resolve operating system paths.

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

11. Functions perform the same function instead of regular expressions.

12. str_replace function is better than preg_The replace function is fast, but the efficiency of the strtr function is str_Four times the replace function.

13. If a string replacement function accepts arrays or characters as parameters and the length of the parameters is not too long, consider writing an extra paragraph of replacement code so that the parameters are one character at a time and not just one line of code to accept arrays as query and replacement parameters.

14. Using a select branch statement is better than using multiple if, else if statements.

15. Blocking error messages with @ is very inefficient and extremely inefficient.

16. Open mod_of apachedeflate module, can improve the speed of web browsing.

17. The database connection should be closed when it is used up. Do not use long connections.

18. Error messages are expensive.

19. Incrementing local variables in the method is the fastest.It is almost as fast as calling a local variable in a function.

20. Increasing a global variable is two times slower than increasing a local variable.

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

22. Increasing an undefined local variable is 9 to 10 times slower than increasing a predefined local variable.

23. Defining only one local variable without calling it in a function also slows down (to an extent equivalent to increasing one local variable).PHP will probably check to see if there are global variables.

24. The method call does not appear to be related to the number of methods defined in the class because I added 10 methods (both before and after the test method), but the performance did not change.

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

26. Calling an empty function with one parameter takes the same amount of time as performing 7 to 8 local variable increments.Similar method calls take nearly 15 local variable increment operations.

27. Apache parses an PHP script 2 to 10 times slower than parsing a static HTML page.Use as many static HTML pages as possible and fewer scripts.

28. Unless the script can be cached, it will recompile once per call.Introducing a set of PHP caching mechanisms typically improves performance by 25% to 100% to avoid compilation overhead.

29. Cache as much as possible using memcached.memcached is a high performance memory object caching system that can be used to speed up dynamic Web applications and reduce database load.Caching of opcodes (OP code) is useful so that scripts do not have to recompile each request.

30. When you manipulate a string and need to verify that its length meets certain requirements, you naturally use the strlen() function.This function executes fairly quickly 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 the PHP variable).However, since strlen () is a function, it is more or less slower because function calls go through a number of steps, such as lowercase (note: function names are lowercase, PHP is not case sensitive), hash lookup, and follow called function 1.In some cases, you can use the isset () technique to speed up your code execution. (An example is as follows) if (strlen ($foo) < 5) {echo "Foo is too short" $} (compared 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 it does not require function lookup and lowercase.That is, you don't actually spend much on top-level code that checks string length.

31. When the execution variable $i increases or decreases, $i++ will be a little slower than ++$i.This difference is unique to PHP and does not apply to other languages, so please do not modify your C or Java codes and expect them to be faster and useless immediately. +$i is faster because it only requires three instructions (opcodes), $i++ requires four.Post-increment actually produces a temporary variable that is then incremented.Pre-increment increases directly on the original value.This is one of the optimizations, as the PHP optimizer for Zend does.It's a good idea to keep in mind that not all instruction optimizers do the same optimization, and there are a large number of Internet service providers (ISPs) and servers that don't have an instruction optimizer installed.

32. Object-oriented is not necessarily object-oriented (OOP). Object-oriented is often expensive and consumes a lot of memory for each method and object call.

33. Arrays are also useful, rather than using classes to implement all data structures.

34. Don't break down your methods too much. Think carefully about what codes you really want to reuse.

35. You can always break code down into methods when you need them.

36. Use as many PHP built-in functions as possible.

37. If you have a large number of time-consuming functions in your code, you can consider implementing them with the C extension.

38. Evaluate test (profile) your code.The inspector tells you what parts of the code are consuming how much time.The Xdebug debugger contains a validation program that can generally show code bottlenecks.

39. mod_zip can be used as an Apache module to compress your data instantly and reduce data transmission by 80%.

40. When file_is availableget_When contents replaces the file, fopen, feof, fgets series of methods, try to use file_get_contents, because he is much more efficient!But note file_get_contents version of PHP when opening an URL file;

41. Minimize file operations, although the file operation efficiency of PHP is not low;

42. Optimize the Select SQL statement and perform as few Insert and Update operations as possible (I was hated on update);

43. Use the internal functions of PHP whenever possible (but I wasted time writing out a custom function in order to find a function that does not exist in PHP, experience question!);

44. Do not declare variables inside the loop, especially large variables: objects (does this seem to be more than just an issue in PHP?);

45. Multidimensional arrays should not loop nested assignments as much as possible.

46. Do not use regular expressions when you can manipulate functions with PHP internal strings;

47. foreach is more efficient. Replace while and for cycles with foreach whenever possible.

48. Use single quotation marks instead of double quotation marks to refer to strings;

49. "Replace i=i+1 with i+=1."Compliant with c/c++ habits and efficient"

50. For global variables, unset() should be used up.


Related articles: