Php programs must look at the optimization method

Source: Internet
Author: User
Tags arrays data structures lowercase memcached php and php script php tutorial strlen

Php tutorial programs must look at the optimization method

1. If a method can be static, it will be declared as static. The speed can be increased to 4 times.

2. echo is faster than print.

3. Use multiple echo parameters instead of periods to replace string connections.

4. Determine the maximum number of cycles before executing the for loop. Do not calculate the maximum value for each loop.

5. Cancel unnecessary variables, especially large arrays, to release the memory.

6. Avoid using _ get ,__ set ,__ autoload whenever possible.

7. require_once () is expensive.

8. If you use the full path when the file is included, it takes less time to parse the operating system path.

9. If you want to know the time when the script starts to be executed (the server receives the client request), use $ _ server ['request _ time'] instead of time ().

10. Functions use the same functions instead of regular expressions.

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 can take an array or character as a parameter, and the parameter length is not too long, you can consider writing an additional replacement code so that each parameter passing is a character, instead of writing only one line of code to accept arrays as query and replacement parameters.

13. Using the select branch statement is better than using multiple if and else if statements.

14. Blocking error messages with @ is very inefficient.

15. Open the mod_deflate module of apache.

16. The database tutorial connection should be closed after use.

17. $ row ['id'] is 7 times the efficiency of $ row [id.

18. The error message is expensive.

19. do not use functions in a for loop, such as for ($ x = 0; $ x <count ($ array); $ x). The count () function is called once every loop.

20. Increase the local variable in the method at the fastest speed. It is almost the same as calling a local variable in a function.

21. Increasing a global variable is twice slower than increasing a local variable.

22. Incrementing an object property (for example, $ this-> prop ++) is three times slower than incrementing a local variable.

23. Increasing an unspecified local variable is 9 to 10 times slower than increasing a predefined local variable.

24. Defining only one local variable without calling it in the function also slows down (to an extent equivalent to increasing a local variable ). Php will probably check whether global variables exist.

25. Method calling seems to have nothing to do with the number of methods defined in the class, because I have added 10 methods before and after the test method, but the performance has not changed.

26. The methods in the derived class run faster than the same methods defined in the base class.

27. Calling an empty function with a parameter takes seven to eight times to increment local variables. Similar method calls take nearly 15 times to increment local variables.

28. Use single quotes instead of double quotes to include strings, which is faster. Because php searches for variables in strings enclosed by double quotation marks, but not in single quotes. Of course, this can be done only when you do not need to include a variable in a string.

29. When multiple strings are output, use commas instead of periods to separate strings, which is faster. Note: Only echo can do this. It is a "function" that can treat multiple strings as parameters, so the function is added with double quotation marks ).

30. apache parses a php script two to ten times slower than parsing a static html page. Use static html pages and less scripts as much as possible.

31. Unless the script can be cached, it will be re-compiled every time it is called. The introduction of a php Cache mechanism can generally improve the performance by 25% to 100%, so as to avoid compilation overhead.

32. Use memcached as much as possible. Memcached is a high-performance memory object cache system that can accelerate dynamic web applications and reduce database load. It is useful for the cache of op code, so that the script does not have to re-compile each request.

33. When operating on a string and checking whether its length meets certain requirements, you use the strlen () function. This function is executed quite quickly because it does not perform any calculations and only returns the known string length stored in the zval structure (c's built-in data structure, used to store php variables. However, because strlen () is a function, it is more or less slow, because function calls take many steps, such as lowercase letters, php does not distinguish between case-insensitive function names.) and hash searches are executed along with the called function. In some cases, you can use the isset () technique to accelerate your code execution.

(For example)
If (strlen ($ foo) <5) {echo "foo is too short ";}
(Compare with the following tips)
If (! Isset ($ foo {5}) {echo "foo is too short ";}

Calling isset () happens to be faster than strlen (), because unlike the latter, isset () is used as a language structure, this means that function search and lowercase letters are not required for execution. That is to say, in fact, you do not spend too much money in the top-level code that checks the string length.

34. When the execution variable $ I increments or decreases, $ I ++ is slower than ++ $ I. This difference is exclusive to php and does not apply to other languages. Therefore, do not modify your c or java code and expect them to become faster and useless immediately. ++ $ I is faster because it only requires three commands (opcodes), and $ I ++ requires four commands. In fact, a temporary variable is generated in post-increment mode, which is then incremented. The pre-increment directly increases on the original value. This is a kind of optimization, as the zend php optimizer does. It is a good idea to keep this optimization process in mind, because not all command optimizers perform the same optimization and there are a large number of Internet service providers (isps tutorial) without command optimizers) and server.

35. Object-oriented (oop) is not required. Object-oriented usually has a high overhead, and each method and object call consumes a lot of memory.

36. Arrays are also useful instead of using classes to implement all data structures.

37. Do not subdivide the methods too much. Think carefully about the code you actually intend to reuse?

38. 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 the code, you can consider using c extension to implement them.

41. profile your code. The validator will tell you how much time the code consumes. The xdebug debugger contains an inspection program, which can display the code bottleneck in general.

42. mod_zip can be used as an apache module to instantly compress your data and reduce the data transmission volume by 80%.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.