In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to improve the efficiency of PHP implementation, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. It is faster to include strings in single quotation marks instead of double quotation marks. Because PHP will search for variables in strings surrounded by double quotes, while single quotes will not, note: only echo can do this, it is a "function" that can take multiple strings as parameters. (translation note: PHP manual says that echo is a language structure, not a real function, so put the function in double quotes).
2. If the method of a class can be defined as static, try to define it as static, and its speed will be nearly 4 times faster.
3. The speed of $row ['id'] is seven times that of $row [id].
4. Echo is faster than print and uses multiple parameters of echo instead of string concatenation, such as echo $str1,$str2.
5. Determine the maximum number of cycles before executing the for loop. Do not calculate the maximum value every time. It is best to use foreach instead.
6. Log out unused variables, especially large arrays, in order to free memory.
7. Avoid using _ _ get,__set,__autoload as much as possible.
8. Require_once () is expensive.
9. Include files use absolute paths as far as possible, because it avoids the speed at which PHP looks for files in include_path, and it takes less time to parse the operating system path.
10. If you want to know when the script starts to execute, use the
$_ SERVER ['REQUEST_TIME'] is better than time ()
11. Functions perform the same function instead of regular expressions.
The str_replace function is faster than the preg_replace function, but the strtr function is four times as efficient as the str_replace function.
13. If a string replacement function accepts arrays or characters as parameters, and the parameter length is not too long, you can consider writing an additional replacement code so that each passing parameter is one character. Instead of just writing a line of code, you can accept the array as a query and replacement parameter.
14. It is better to use a selective branch statement (switch case) than to use multiple if,else if statements.
15. The practice of blocking error messages with @ is very inefficient, extremely inefficient.
16. Open the mod_deflate module of apache to improve the browsing speed of the web page.
17. The database connection should be turned off when it is finished, and do not use a long connection.
Error messages are expensive.
19. It is the fastest to increment local variables in the method. It is almost as fast as calling a local variable in a function.
20. Incrementing a global variable is twice as slow as increasing a local variable.
21. Incrementing an object property (such as $this- > prop++) is 3 times slower than incrementing a local variable.
22. Incrementing an undefined local variable is 9 to 10 times slower than incrementing a predefined local variable.
23. Defining a local variable without calling it in the function also slows down (to the extent equivalent to incrementing a local variable). PHP will probably check to see if there are global variables.
Method calls seem to have nothing to do with the number of methods defined in the class, because I added 10 methods (both before and after testing the methods), but there was no change in performance.
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 time as performing 7 to 8 local variable increment operations. The time taken by a similar method call is close to 15 local variable increment operations.
27. Apache parses a PHP script 2 to 10 times slower than a static HTML page. Try to use more static HTML pages and fewer scripts.
28. Unless the script can be cached, it will be recompiled each time it is called. The introduction of a PHP caching mechanism can typically improve performance by 25 to 100 percent, eliminating compilation overhead.
29. Try to cache, you can use memcached. Memcached is a high-performance in-memory object caching system that can be used to accelerate dynamic Web applications and reduce database load. Caching of OP code is useful so that scripts do not have to be recompiled for each request.
When you manipulate a string and need to verify that its length meets certain requirements, you take it for granted that you will use the strlen () function. This function is fairly fast because it doesn't do any calculations and only returns the known string length stored in the zval structure (C's built-in data structure for storing PHP variables). However, because strlen () is a function, it is more or less slow because function calls go through many steps, such as lowercase letters, hash lookups, and hash lookups that follow the called function. In some cases, you can use the isset () technique to accelerate the execution of your code.
(examples are as follows)
The copy code is as follows:
If (strlen ($foo) < 5) {echo "Foo is too short" $$}
(compare with the following techniques)
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 construct, means that its execution does not require function lookups and lowercase letters. That is, you don't actually spend much money on top-level code that checks the length of a string.
31. When executing the increment or decrement of the variable $I, $I will be slower than + + $I. This difference is specific to PHP and does not apply to other languages, so please do not modify your C or Java code and expect them to be faster immediately. + + $I is faster because it requires only three instructions (opcodes), while $iTunes + requires four instructions. The post increment actually produces a temporary variable, which is then incremented. The pre-increment increases directly on the original value. This is one of the most optimized processes, 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, and there are a large number of Internet service providers (ISPs) and servers that do not have instruction optimizers.
32. You don't have to be object-oriented (OOP). Object-oriented is often very expensive, and each method and object call consumes a lot of memory.
33. Not all data structures are implemented with classes, and arrays are also useful.
Don't break down the methods too much, think about what code you really intend to reuse.
When you need it, you can always break down the code into methods.
36. Try to use a large number of PHP built-in functions.
37. If there are a large number of time-consuming functions in your code, you can consider implementing them in a C extension.
38. Profile your code. The verifier will tell you which parts of the code take how much time. The Xdebug debugger includes a verification program, and the evaluation verification can show the bottleneck of the code in general.
39. Mod_zip can be used as an Apache module to compress your data in real time and reduce the amount of data transmission by 80%.
40. When file_get_contents can be used instead of file, fopen, feof, fgets and other methods, try to use file_get_contents, because it is much more efficient! But pay attention to the PHP version of file_get_contents when opening a URL file.
41. Do as few file operations as possible, although the file operation efficiency of PHP is not low.
42. Optimize Select SQL statements and do as few Insert and Update operations as possible (on update, I have been maliciously criticized)
43. Use PHP internal functions as much as possible (but I wasted the time I could have written a custom function in order to find a function that doesn't exist in PHP, empirical problems!)
44. Don't declare variables inside the loop, especially large variables: objects (it seems that this is not just a problem to pay attention to in PHP, is it?)
45. Try not to have cyclic nesting assignments for multidimensional arrays.
46. Do not use regular expressions when you can manipulate functions with PHP internal strings
47. Foreach is more efficient, using foreach instead of while and for cycle as far as possible
48. Replace double quotation marks with single quotation marks to quote strings
49. "replace i=i+1 with iTunes 1. It is in line with the habit of cUniverse plus, and it is still efficient."
50. For the global variable, you should unset () as soon as you finish using it.
51. Use checkdnsrr () to confirm the validity of some email addresses through the existence of domain names. This built-in function ensures that each domain name corresponds to an IP address.
52. If you are using php5 and mysql4.1 or above, consider using mysqli_, an improved function of mysql_
Use highlight_file () to automatically print a well-formatted copy of the page source code
Use the error_reporting (0) function to prevent potentially sensitive information from being displayed to the user. Ideal error reporting should be completely disabled in the php.ini file. But if you are using a shared virtual host and you cannot modify php.ini, you'd better add the error_reporting (0) function on the first line of each script file (or load it with require_once ()). This can effectively protect sensitive SQL queries and paths from being displayed in the event of an error.
Use the error_reporting (0) function to prevent potentially sensitive information from being displayed to the user. Ideal error reporting should be completely disabled in the php.ini file. But if you are using a shared virtual host and you cannot modify php.ini, you'd better add the error_reporting (0) function on the first line of each script file (or load it with require_once ()). This can effectively protect sensitive SQL queries and paths from being displayed in the event of an error.
56. Make a function have multiple return values by referencing the address of the parameter variable. You can add a "&" to the variable to indicate that it is passed by address rather than by value.
57. To turn off the PHP Magic quotation marks (Magic Quote) function, not every piece of escaped data has to be inserted into the database. If all the data entering PHP are escaped, it will have a certain impact on the execution efficiency of the program. It is more efficient to call escape functions (such as addslashes ()) at run time
58. Use the ip2long () and long2ip () functions to convert IP addresses into integers and store them in the database instead of characters. This can almost reduce the storage space of 1 pound by 4. At the same time, it is easy to sort and quickly find addresses.
Use gzcompress () and gzuncompress () to compress (decompress) large-capacity strings when saving (taking out) the database. This built-in function can be compressed to 90% using the gzip algorithm
60. Use checkdnsrr () to confirm the validity of some email addresses through the existence of domain names. This built-in function ensures that each domain name corresponds to an IP address.
61. Isset is used instead of strlen in some places
When you manipulate a string and need to verify that its length meets certain requirements, you take it for granted that you use the strlen () function. This function is fairly fast because it doesn't do any calculations and only returns the known string length stored in the zval structure (C's built-in data structure for storing PHP variables). However, because strlen () is a function, it is more or less slow because function calls go through many steps, such as lowercase letters, hash lookups, and hash lookups that follow the called function. In some cases, you can use the isset () technique to accelerate the execution of your code.
(examples are as follows)
If (strlen ($foo) < 5) {echo "Foo is too short" $$} (compare with the following technique) 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 construct, means that its execution does not require function lookups and lowercase letters. That is, you don't actually spend much money on top-level code that checks the length of a string.
The above is all the contents of the article "how to improve the efficiency of PHP implementation". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.