php Notes | Points

1, 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, pay attention: only works with echo, which is a can take several strings as arguments "function" (translation: PHP Manual He said echo is a language construct, not a real function, so the function plus the double quotes).

2, if the method can be defined as static class, defined as static as far as possible, it is nearly four times the speed will increase.

. 3, R & lt O W [ ' I D ' ] is the speed of a row [id] 7 times.

. 4, print faster than the echo, and the use of multiple echo parameters: Instead of connecting string (Annotation refers comma instead of a period), such as echo S T R & lt . 1 , str2. 

5, determined prior to execution for the maximum number of loop cycles, not in the loop will calculate the maximum, best use foreach instead.

6, cancellation of your variables, especially large arrays in order to free up memory.

7, try to avoid using __get, __ set, __ autoload.

8, require_once () is expensive.

9, try to use absolute paths when include file, because it avoids the PHP include_path to find files in speed, the time required to resolve the OS paths less.

10, if you want to know the script started executing: time (ie Annotation server client request is received), the use of

11, instead of the regular expression functions perform the same function.

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

13, if a string replacement function, accepts both arrays and characters as arguments, and the parameter is not too long, it may be considered an additional write a replacement code, so that each passing one character, instead of one line of code that accepts arrays as a parameter search and replace. 

14, using select statements (translation: switch case) is better than using multiple if, else if statements.

15, muting error message with @ approach is very inefficient, extremely inefficient.

16, open the apache module mod_deflate can increase Web browsing speed.

17, should be used when a database connection is finished off, do not use a long connection.

18. Error messages are expensive.

19. Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in the function.

20, incrementing a global variable slower than a local variable twice.

2 . 1 , delivered by a th pair of object genera properties ( eg : this-> prop ++) slower than a local variable three times.

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

23. Just declaring a global variable 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.

24, 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 

25, a method in a derived class running faster in the same manner as defined in the base class.

26, calling a null function with 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.

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

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

29, try to do 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.

30, when the need to test the operation 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”;


(与下面的技巧做比较) 
if (!isset($foo{5})) { 
  echo “Foo is too short”;
}

Calling isset () happens () is faster than strlen, because unlike the latter is, isset () is a language construct, meaning that it's execution does not require function lookups and lowercase technology. That, in fact, the top-level code that determines the length of the string you did not spend too much overhead.

31, when performing the variable i is handed increase or delivery Save time , than i ++ ++ i slower a few . This kind of difference different is P H P special there are , and not Shi Yong to its his language words , that in order to please not to repair the change you a C or J A v A generation of code and refers to the hope that they canLi i.e. becomes fast , no use of . + + 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.

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

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

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

35, when you need to, you can always split the code of the method.

36, as far as possible a large number of built-in functions PHP.

37, if the function is very time consuming in the code, you can consider them as C extensions.

38, 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.

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

40, in the case of an alternative file, fopen, feof, fgets, and other methods using file_get_contents, try to use file_get_contents, because he's much more efficient but be careful file_get_contents PHP version of the problem in opening a URL file time!;

41, be minimal and file operations, although the PHP file operations efficiency is not low;

42, optimization Select SQL statement, in the case where possible to minimize the Insert, Update operations (on the update, I was too bad batch);

43, as much as possible the use of PHP internal functions (but I have to find a PHP function which does not exist, this waste can write a custom function of time, experience problems ah!);

44, the inner loop do not declare variables, especially the large variable: Object (? It just does not seem to pay attention to the problem of PHP inside it);

45, multi-dimensional arrays try not nested loop assignment;

46, in the case of a possible inside PHP string manipulation functions, do not use regular expressions;

47, foreach more efficient, as far as possible and while in place for use foreach loop;

48, instead of double quotes single quotes string;

49, "i + = 1 Instead of using i = i + 1. Comply c / c ++ diet, but also high efficiency"

50, for global variables should be spent on the unset () off;

Guess you like

Origin www.cnblogs.com/gaosf/p/11102127.html