Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to improve the performance of PHP

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will give you a detailed explanation on how to improve the performance of PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Use single quotation marks instead of double quotation marks to include strings, which is faster. 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).

1. If you can define the method of a class as static, try to define it as static, which will be nearly 4 times faster.

2. The speed of $row ['id'] is seven times that of $row [id].

3. Echo is faster than print and uses multiple parameters of echo instead of string concatenation, such as echo $str1,$str2.

4. 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.

5. Log out unused variables, especially large arrays, in order to free memory.

6. Avoid using _ _ get,__set,__autoload as much as possible.

7. Require_once () is expensive.

8. 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.

9. If you want to know when the script starts to execute, using $_ SERVER ['REQUEST_TIME'] is better than time ().

10. The function performs the same function instead of the regular expression.

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

12. 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 one line of code, you can accept an array as a query and replacement parameter.

13. It is better to use a selective branch statement (switch case) than to use multiple if,else if statements.

14. The practice of blocking error messages with @ is very inefficient, extremely inefficient.

15. Open the mod_deflate module of apache to improve the browsing speed of the web page.

16. The database connection should be turned off when it is finished, and do not use a long connection.

17. Error messages are expensive.

18. It is the fastest to increment local variables in the method. It is almost as fast as calling a local variable in a function.

19. Incrementing a global variable is twice as slow as increasing a local variable.

20. Incrementing an object property (such as $this- > prop++) is 3 times slower than incrementing a local variable.

21. Incrementing an undefined local variable is 9 to 10 times slower than incrementing a predefined local variable.

22. 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.

23. 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.

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

25. 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.

26. Apache parses a PHP script 2 to 10 times slower than parsing a static HTML page. Try to use more static HTML pages and fewer scripts.

27. 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.

28. Cache as much as possible, 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.

29. When you manipulate a string and need to verify that its length meets a certain requirement, you will naturally 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 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.

34. 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.

35. 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.

36. Not all data structures are implemented with classes, and arrays are also useful.

37. 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.

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

40. If there are a large number of time-consuming functions in your code, you can consider implementing them in a C extension.

41. 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.

42. 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%.

43. 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.

44. Do as few file operations as possible, although the file operation efficiency of PHP is not low.

45. Optimize Select SQL statements and perform as few Insert and Update operations as possible

46. 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!)

47. No * * 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?)

48. Try not to have cyclic nesting assignments for multidimensional arrays.

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

50. Foreach is more efficient, using foreach instead of while and for cycles as far as possible

51. Replace double quotation marks with single quotation marks to quote strings

52. "replace i=i+1 with iTunes 1. It is in line with the habit of cUniverse plus, and it is still efficient."

53. For the global variable, unset () should be dropped when it is used up.

-

1. Speak to many code files, especially containing a lot of include or require files. They take more time and parsing and generate intermediate code.

2. Even if the PHP code file has not changed, the execution process will be carried out strictly according to the process. That is, regardless of whether your program has changed or not, you need to recompile and generate opcode each time you call it. (in fact, this is the reason why the compilation cache exists.)

3. This process occurs not only in the main code files, but also in every include and require. (this can continue to be optimized)

Can be maintained with APC

This is the end of the article on "how to improve the performance of PHP". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report