|
1. Try to static:
If a method can be static, then it is declared as static, 1/4 speed can be increased, even when I tested, this increased nearly threefold.
Of course, this test method requires the execution of more than one hundred thousand, the effect was obvious.
In fact, the efficiency of static methods and non-static method of the main differences in memory: the static method generates at the beginning of the program memory, the method generates an instance in memory the program is running, so you can directly call the static method, instance method first instance a student, by way of example calling method, static fast, but will account for more memory.
Any language of memory and disk operations, as to whether the object-oriented, just a matter of software layers, the bottom is the same, but the implementation is different. Static memory is continuous, because it is at the beginning of the program is generated, and examples of the application of discrete space, so of course there is no fast static method.
Static methods always call the same memory, the drawback that they can not automatically be destroyed, but can be instantiated destroyed.
2.echo efficient than print, because there is no echo return values, print return an integer;
test:
Echo
0.000929 - 0.001255 s (average of 0.001092 seconds)
Print
0.000980 - 0.001396 seconds (average of 0.001188 seconds)
A difference of about 8%, the overall echo is relatively fast.
Note, echo a string of big, if it does not seriously affect the performance to make adjustments. Use of open apached mod_deflate compress or open ob_start first content into the buffer.
3. Before the cycle sets the maximum number of cycles, rather than in the loop;
Fool understand.
4. Destruction variables to free memory, especially large arrays;
Arrays and objects in php special account for memory, and this due to the php zend engine of the underlying cause,
In general, PHP array memory utilization only 1/10, that is to say, an array in C language, 100M of memory, which is necessary in PHP 1G.
Especially in PHP as a back-end server systems, often run out of memory consumed much of a problem.
5. Avoid the use of like get, __set, __autoload and other magic methods;
For the beginning of the function is named magic functions, these functions under certain conditions at the beginning of the visit. Generally speaking, are the following magic function __construct (), __ destruct (), __ get (), __ set (), __ unset (), __ call (), __ callStatic (), __ sleep (), __ wakeup (), __ toString () , __ set_state (), __ clone (), __ autoload ()
In fact, if __autoload not efficient class name to the actual disk file (Note that this refers to the actual disk file, not just the file name) in association with, the system will have to do a lot of file exists (in every need a path include path included to look for) the judgment, and determine whether a file exists to do disk I / O operations, it is known disk I / O operation efficiency is very low, so this is so autoload mechanism reduced efficiency reasons.
Therefore, we in the system design, the need to define a clear mechanism of the class name and the actual disk file mapping. The simpler the rules more clearly, the higher the efficiency autoload mechanism.
Conclusion: Not a natural low efficiency autoload mechanism, only abuse autoload, poorly designed automatic loading function will lead to a decrease in efficiency.
So try to avoid using __autoload magic method, open to question.
6.requiere_once () more consumption of resources;
This is because requiere_once need to determine whether the file is cited), you can not try to do. Common require / include ways to avoid.
7. Use the absolute path includes and requires the.
If a relative path, PHP will find the file in the include_path traverse inside.
Use absolute paths will avoid such problems, and therefore the time required to resolve the path to the operating system will be less.
8. If you need time to get the script execution time, $ _ SERVER [ 'REQUSET_TIME'] better than time ();
Conceivable. Is a ready-made can be directly used, the results also need a function derived.
9. The case of internal use PHP string manipulation functions, try to use them, do not use regular expressions; because it is more efficient than regular;
I did not have to say, the regular consumption of most properties.
There is no useful function that you missed? For example: strpbrk () strncasecmp () strpos () / strrpos () / stripos () / strripos () acceleration strtr If you need to convert a single character all the time,
A string instead of an array do strtr:
'E',)); // bad>?
Efficiency: 10 times.
10.str_replace character replacement than preg_replace regular replacement fast, but fast strtr than str_replace 1/4;
Also do not do unnecessary replacement if not replace, str_replace will allocate memory for parameter. very slow! Solution:
With strpos to find (very fast) to see if you need to replace, if needed, and then replace efficiency: - If you need to replace: the efficiency is almost equal to the difference of about 0.1%.
If you do not replace: strpos with 200% faster.
11. The parameter is a string
If a function is not only accepted but also accepts an array of characters as simple parameters, such as character replacement functions, and the argument list is not too long, consider writing a few redundant replacement code, so that each time a character is to pass parameters, rather than accepting an array as the Find and replace parameter. Trivialize, 1 + 1> 2;
12. better not @ @ error with cover will reduce the speed of the script;
@ In fact there are many with a background operation. Compared with no @ @, efficiency gap: 3 times. Do not use the @ in particular cycle, the fifth cycle of the test, even if is to use error_reporting (0) to turn off the error, and then open the cycle is completed faster than with @.
13. $ row [ 'id'] than $ row [id] 7 times faster
An array of key recommendations to develop the habit of quotation marks;
14. Do not use the function in the loop
For example For ($ x = 0; $ x
16. The establishment of a local variable in speed in the fastest class method, and almost calling a local variable in the method of Lane as fast;
17. The establishment of a global variable slower than a local variable 2 times;
Due to the existence of local variables on the stack, when a function takes up stack space is not a great time, this memory is likely to hit all the cache, this time the CPU access efficiency is very high.
Conversely, if a function is used in both the use of global variables and local variables, then when the large difference between these two addresses, cpu cache needs to switch back and forth, then the efficiency drops.
(I understand ah)
18. The establishment of an object property (inside the class variable), for example ($ this-> prop ++) slower than a local variable 3 times;
19. The establishment of a local variable is not declared than the one already defined local variables 9-10 times slower
20. a statement is not a function of any global variables used also degrade performance (and the same number of declarations of local variables).
PHP possible to check the global variable exists;
Performance and the number of methods defined inside a class of 21. The method does not matter
Or because I added 10 more methods to the class that tests the performance of no difference after (these methods before and after the test method);
22. In the performance of the sub-class method is better than in the base class;
23. calls only one parameter and function of time it takes to run an empty function body is equal to 7-8 times $ localvar ++ operation, while a similar method (function class) run is equal to about 15 times $ localvar ++ operator;
24 single quotes instead of double quotes to contain the string, this will be faster.
Because PHP searches for variables in the string in double quotes, single quotes are not.
PHP engine allows the use of single and double quotes to encapsulate the string variable, but this is a great difference! Use double quotes strings tell PHP to read the string contents of the first engine to find which variables, and to variable corresponding value. Generally there is no string variables, so the use of double quotes will result in poor performance. Best to use the word
Connection string instead of double-quoted strings.
BAD:
$ Output = "This is a plain string";
GOOD:
$ Output = 'This is a plain string';
BAD:
$ Type = "mixed";
$ Output = "This is a $ type string";
GOOD:
$ Type = 'mixed';
$ Output = 'This is a' $ type 'string'..;
25. When the echo string with a comma instead of dot connector faster.
echo a way to put several strings as arguments of the "function" (translation: PHP manual says echo the structure of language is not really a function, so the function with the double quotes).
Such as echo $ str1, $ str2.
26.Apache A PHP script than a static HTML page 2-10 times slower.
Try to use more static HTML pages and fewer scripts.
28. Try to use the cache, it is recommended to use memcached.
High-performance, distributed memory object caching system to improve the performance of dynamic web applications, reduce the burden on the database;
Also the operation code (OP code) caches are useful so that the script does not have to be compiled on every request.
29. Use ip2long () and long2ip () function to store the IP addresses turn into an integer in the database, rather than character.
This is almost a quarter of the storage space can be reduced. At the same time it can be easily and quickly sort the address lookup;
30. The effectiveness of the use of checkdnsrr () to confirm the email address domain part of existence
This built-in function to ensure that each domain name corresponds to an IP address;
31. Use mysql_ improved function mysqli_;
32. Try to prefer to use the ternary operator (:)?;
33. The need for PEAR
You want to completely redo your project before, look at the PEAR there you need. PEAR is a huge resource library, a lot of php developers know;
35. Use error_reporting (0) function to prevent potentially sensitive information to the user.
Ideal error reporting should be completely disabled in the php.ini file. But if you are using a shared hosting, php.ini you can not modify, then you'd better add error_reporting (0) function, on the first line of each script file (or
require_once () to load) that can effectively protect sensitive SQL queries and path will not be displayed when an error occurs;
36. Use gzcompress () and gzuncompress () string of large capacity compressed (decompressed) in the presence of feed (remove) database.
This built-in functions use the gzip algorithm can be compressed to 90%;
37. Address by the variable parameters have to make reference to a function has multiple return values.
You can add a variable before the "&" to indicate passed by address rather than by value;
Fully understand the magic quotes dangerous and SQL injection.
Fully understand "magic quotes" and the dangers of SQL injection. I'm hoping that most developers reading this are already familiar with SQL injection. However, I list it here because it's absolutely critical to understand. If you've never heard the term before, spend the entire rest of the day googling and reading.
39. In some places use isset instead of strlen
When the operation need to check the length of the string and meets certain requirements, you will assume the use of strlen () function. This function is pretty fast, because it does not do any calculation but merely return the known length of the string stored in zval structure (C built-in data structures used to store variables in PHP). However, because strlen () function is still somewhat slow because the function call requires several steps, such as lowercase letters (Annotation: refers to the name of the function of the lower case, 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.
(Example below)
if (strlen ($ foo) <5) {echo "Foo is too short" $$}
(Compared with the underlying skills)
if (! isset ($ foo {5})) {echo "Foo is too short" $$}
Calling isset () happens to be faster then strlen () because the person is different, isset () is a language construct, meaning that it's execution does not require function lookups and lowercase technology. That is, in fact, the top-level test string length code you do not have to spend too much overhead.
40. Use ++ $ i is incremented
When incrementing or decrementing the value of the variable $ i ++ happens to be a tad slower then ++ $ i. This is something PHP specific and does not apply to other languages, so do not go modifying your C or Java code thinking it ' ll suddenly become faster, it will not. ++ $ i happens to be faster in PHP because instead of 4 opcodes used for $ i ++ you only need 3. Post incrementation actually causes in the creation of a temporary var that is then incremented. While preincrementation increases the original value directly. This is one of the optimization that opcode optimized like Zend's PHP optimizer. It is a still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer.
When the value of the variable $ i is incremented or decremented, $ 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, it will not. ++ $ I is faster because it requires three instructions (opcodes), $ i ++ you need four instructions. Post incrementation actually generates a temporary variable, this temporary variable is then incremented. The pre-incrementation increases the original value directly. This is one of the optimization process, as Zend's PHP optimizer to be. Keep in mind this optimization process is a good idea, and since not all opcode optimizers do the same optimization process, and there are not a lot of assembly instructions Optimizer Internet service
Providers (ISPs) and servers.
Do not copy the variable on
Sometimes PHP code to make more clean, some PHP novice (including me) will copy the predefined variable to a shorter variable name, in fact, this result is a doubling of memory consumption, will only make program more slowly. Just look at the following example, if a malicious user to insert text 512KB byte text input box, this will lead to 1MB of memory being consumed!
BAD:
$ Description = $ _POST [ 'description'];
echo $ description;
GOOD:
echo $ _POST [ 'description'];
41 Using select statements
switch case is better than using a plurality of if, else if statements, and the code easier to read and maintain.
42. In the alternative you can use file_get_contents file, fopen, feof, fgets
Before you can use file_get_contents substitute file, the case of fopen, feof, fgets, and other methods, try to use file_get_contents, because his efficiency is much higher! But beware PHP version file_get_contents problem when opening a URL file;
43. Try fewer file operations, file operations although the efficiency of PHP is not low;
44. Select SQL statement optimization, where possible, to minimize the situation Insert, Update operation (in the update, I was too bad batch);
45. The possible use PHP internal functions
46. Do not declare variables inside the loop, especially the large variables: Objects
(It just does not seem to pay attention to problems inside PHP, right?);
47. Try not to loop nesting multidimensional array assignment;
48.foreach more efficient, try to use foreach instead of while and for loop;
49. "replaced with i + = 1 i = i + 1 in line with c / c ++ habits, but also high efficiency.";
50. The global variables should be spent on the unset () off;
51 do not necessarily object-oriented (OOP), object-oriented often much overhead, each method and object call consumes a lot of memory.
52 Do not split methods too much, I think you will really re which code?
53 If you have very time consuming functions in your code, you can consider them as C extensions.
54, open the apache mod_deflate module can improve web browsing speed.
(Mentioned issues echo large variables)
55, when the database connection should be switched off after use, do not use long connection.
56, split faster than exploade
split ()
0.001813 - 0.002271 seconds (avg 0.002042 seconds)
explode ()
0.001678 - 0.003626 seconds (avg 0.002652 seconds)
Split can take regular expressions as delimiters, and runs faster too. ~ 23% on average. |
|
|
|