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

42 optimization criteria to be paid attention to in PHP development program

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "42 optimization criteria that need to be paid attention to in PHP development program". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "PHP development program need to pay attention to 42 optimization guidelines" it!

PHP's unique syntax is a mix of C, Java, Perl, and PHP's self-innovative syntax. It can execute dynamic web pages faster than CGI or Perl. Compared with other programming languages, the dynamic page made by PHP is executed by embedding the program into the HTML document, and the execution efficiency is much higher than that of CGI which completely generates HTML tags. The optimization criteria of 42 programs are introduced below.

1. If a method can be static, make a static declaration on it. The speed can be increased to 4 times.

2.echo is faster than print.

3. Use multiple parameters of echo instead of string concatenation.

4. Determine the number of * cycles before executing the for loop, and do not calculate the * value each time.

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

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

7.require_once () is expensive.

8. Using the full path when including the file takes less time to resolve 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. Function to perform the same function instead of regular expression.

11.str_replace functions are faster than preg_replace functions, but strtr functions are four times more efficient than str_replace functions.

twelve。 If a string replacement function accepts an array or character as a parameter, and the parameter length is not too long, consider writing an extra piece of replacement code so that the parameter is one character at a time. Instead of just writing a line of code, you can accept an array as a query and replacement parameter.

13. Using a select branch statement (switch case) is better than using multiple if,else if statements.

14. Blocking error messages with @ is very inefficient.

15. Open the mod_deflate module of apache.

16. The database connection should be closed when it is finished.

17.$row ['id'] is seven times more efficient than $row [id].

18. Error messages are expensive.

19. Try not to use functions in for loops, such as for ($xchang0; $x prop++) is three times slower than incrementing a local variable.

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

24. Defining a local variable without calling it in a function also slows down (to the extent equivalent to incrementing a local variable). PHP will probably check to see if there are global variables.

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

twenty-six。 Methods in derived classes run faster than the same methods defined in the base class.

twenty-seven。 Calling an empty function with one parameter takes the same time as performing 7 to 8 local variable increments. The time taken by a similar method call is close to 15 local variable increment operations.

twenty-eight。 It is faster to include strings in single quotation marks instead of double quotation marks. Because PHP searches for variables in strings enclosed in double quotes, single quotes do not. Of course, you can do this only if you don't need to include variables in the string.

twenty-nine。 When outputting multiple strings, it is faster to separate strings with commas instead of periods. Note: only echo can do this, which is a "function" that can take multiple strings as arguments. PHP manual says that echo is a language structure, not a real function, so put the function in double quotation marks.

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

thirty-one。 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.

thirty-two。 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.

thirty-three。 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 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.

thirty-four。 When the increment or decrement of the variable $I is performed, $ibasket + is 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 a form of * * optimizer, 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.

thirty-five。 It is not necessary to be object-oriented (OOP). Object-oriented is often very expensive, and each method and object call consumes a lot of memory.

thirty-six。 Instead of using classes to implement all data structures, arrays are also useful.

thirty-seven。 Don't break down the methods too much, think about what code you really intend to reuse.

thirty-eight。 When you need it, you can always break down the code into methods.

thirty-nine。 Try to use a large number of PHP built-in functions.

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

forty-one。 Evaluate and 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%.

At this point, I believe you need to pay attention to the "PHP development program 42 optimization criteria" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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