Some suggestions to optimize PHP code

1. If a method can be static, declare it static. Rate may be up to 4 times.

2.echo faster than print. (shorthand echo "<? PHP $ Color = 'Red';?> <= $ Color??>")

3. using multiple echo parameters (translation: means comma instead of a period) instead of the string concatenation. (Example: echo 'a', 'b ', 'c'; // output ABC)

4. determining the maximum number of cycles before the for loop, not in the loop are calculating the maximum value.

5. Log off your variables, especially large arrays in order to free up memory.

6. avoid using __get, __ set, __ autoload.

7.require_once () expensive.

8. Use in includes the full path, the time required to resolve path operating system will be less.

9. If you want to know the script started executing: time (ie Annotation server client request is received), the use of $ _SERVER [ 'REQUEST_TIME'] is better than the time ().

10. regex function instead perform the same function.

11.str_replace function faster than preg_replace, but strtr is four times the str_replace function.

    Example: strtr (string, from, to ) , or strtr (String, Array (= from> to))

12. The function if a replacement string, or character array acceptable as a parameter and the parameter is not too long, it may be considered additional write a replacement code, so that each passing one character, rather than just write a line of code that accepts arrays as search and replace arguments.

13. Use select statements (translation: switch case) is better than using a plurality if, else if statement.

14. Error message with @ shield approach is very inefficient.

15. Open the apache mod_deflate module. (mod_deflate module provides DEFLATEan output filter, allows the server to the client before transmitting the compressed output content to save bandwidth)

16 should be used when a database connection is finished off.

17. $ row [ 'id'] is the efficiency of $ row [id] 7 times.

18. Error messages are expensive.

19. Do not use functions in a for loop, such as for ($ x = 0; $ x <count ($ array); $ x) will be called once per cycle count () function.

20. Incrementing a local variable in the process, is the fastest. Nearly the same as calling a local variable in the function.

21. incrementing a global variable slower than a local variable 2 times.

22. Incrementing an object property (eg: $ this-> prop ++) than a local variable is slower than 3 times.

23. incrementing a local variable is not predefined increments than a predefined local variable is 9-10 times slower.

24. Only a local variable is defined without using it in a function also slows things down (same amount as incrementing a local variable). PHP probably does a check to see if the global exists.

25. The method invocation appears to be independent of the number of methods defined in the class, because I was (before and after the test method) added 10 more methods, but no change in performance.

26. A method of running a derived class defined in the same manner as the base class to be faster.

27. The empty function call with a parameter corresponds to the time it takes to perform 7-8 times localvar operation. A similar method call time spent close to 15 localvar operation.

28. The single quotes instead to contain the string, to do so will be faster. Because PHP looks for variables inside of double quotes, the single quotation marks will not. Of course, only if you do not need to have variables in the string you can do.

29. outputting a plurality of strings, instead of dot comma delimited string faster. Note: only works with echo, which is a can take several strings as arguments "function" (translation: PHP manual says that echo language construct, not a real function, so the function plus the double quotes ).

30.Apache A PHP script than a static HTML page 2-10 times slower. Try to use more static HTML pages and fewer scripts.

31. Unless the script can be cached, or when each call will be recompiled. Install a PHP caching mechanism can usually upgrade 25-100% performance by removing compile times.

32. Try to make the cache, you can use memcached. memcached is a high-performance memory object caching system intended to speed up dynamic Web applications by alleviating database load. Cache of the operation code (OP code) is useful so that the script does not have to recompile for each request.

33. When the need to test operation of the string and its length meets certain requirements, assume that you would use strlen () function. This function is pretty fast, because it does not do any calculations, only returns the length of the string is known in the zval structure (C built-in data structures used to store variables in PHP). However, because strlen () is a function, it is still somewhat slow because the function call requires several steps, such as lowercase letters (Annotation: refers to the lowercase name of the function, PHP function names are not case-sensitive), hash lookup, will follow the function is called to perform together. In some cases, you can use the isset () technique to accelerate the execution of your code.

(Examples below)
IF (strlen ($ foo) <. 5) {echo "Foo IS TOO Short";}
(with the following techniques compare)
(! Isset ($ foo {. 5})) IF {echo "Foo IS TOO short ";}

calling isset () happens () faster than strlen, since unlike the latter is, isset () is a language construct, meaning that it performs no function lookup and lowercase. That, in fact, the top-level code that determines the length of the string you did not spend too much overhead.

34. When performing the variable $ i is incremented or decremented when, $ i ++ i will be slower than some of the $ ++. This is something PHP specific and does not apply to other languages, so please do not go modifying your C or Java code thinking it'll suddenly become faster, useless. ++ $ i is faster because it only requires three instructions (opcodes), $ i ++ you will need four instructions. Postincrement will actually generate a temporary variable, this temporary variable is then incremented. The pre-incrementation increases the original value directly. This is one of the optimization process, so as Zend's PHP optimizer made. Remember that this optimization would be a good idea, since not all opcode optimizers do the same optimization, and there are Internet service providers (ISPs) without an opcode optimizer a lot and servers.

35. objects do not necessarily oriented (OOP), object-oriented often much overhead, each method and object call consumes a lot of memory.

36. Do not implement all of the data structures, arrays are also useful.

37. Do not split methods too much, I think you will really re which code?

38. When you need, you can always split the code of the method.

39. Try using a large number of PHP built-in functions.

40. If there is time consuming functions in your code, you can consider them as C extensions.

41. assessment test (profile) your code. Checker will tell you which parts of your code consumes much time. Xdebug debugger includes testing procedures to assess the bottlenecks can be displayed on the test overall.

42.mod_zip available as an Apache module compresses your data, and allows data transmission volume reduced by 80%.

Reproduced in: https: //www.cnblogs.com/caly/archive/2012/01/29/2331313.html

Guess you like

Origin blog.csdn.net/weixin_33851177/article/details/93538090