Summary of methods for optimizing the PHP program

  • 2020-05-12 02:23:52
  • OfStack

If a method c++an be static, declare it static. Speed improvement is by a factor of 4. If a method can be static, declare it static. The rate can be increased to 4 times.

2. echo is faster than print. echo is faster than print.

3. Use echo's multiple parameters instead of string concatenation. Use multiple arguments of echo instead of string concatenation.

4. Set the maxvalue your maxvalue your your your your before and not in the loop. Determine the maximum number of loops before executing the for loop.

5. Unset your variables to memory, especially large arrays.

Avoid using s 67en magic like like like, s 71en, s 72en, s 7en, s 7en.

7. require_once() is expensive require_once() is expensive.

8. Use full paths includes and requires, less time spent on the OS OS paths. Use the full path when including files, and it will take less time to parse the operating system path.

9. If you to find the time when when started executing, $_SERVER['REQUEST_TIME'] is preferred to time() When the server receives a client request), it is better to use $_SERVER['REQUEST_TIME'] rather than time().

10. See if you use strncasecmp, strpbrk stripos instead instead of regex. Check whether you can use strncasecmp,strpbrk,stripos functions instead of regular expressions to do the same thing.

11. str_replace is faster than preg_replace, but strtr is is faster a a of 4. str_replace function is faster than preg_replace function, But the strtr function is four times as efficient as the str_replace function.

12. If the function, such as string replacement string replacement function, accepts both arrays characters as arguments arguments and if your list is echo list is echo list is echo list echo is echo is echo is echo is echo is echo is echo list echo is echo is echo is echo is echo is echo echo list echo is instead of line of that that that that that that that that that that that that that that that that that that that that that that that that that that that Instead of just writing 1 line of code to accept an array as a parameter to query and replace.

13. It's better to statements statements multi if, else if, statements. It is better to use a select branch statement than multiple if,else if statements.

14. Error suppression with @is very slow. Blocking error messages with @ is very inefficient.

15. Turn on apache s mod_deflate open mod_deflate module of apache.

16. Close your database id you re done with them.

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

18. Error messages are expensive. Error messages are expensive.

19. Do not use functions inside of for loop, such as for ($x=0; $x < count ($array); x) The count() function called each time. Try not to use functions in for loops, such as for ($x=0; $x < count ($array); $x) calls the count() function every loop.

20. Incrementing a a method method the the variable a function Incrementing Incrementing Incrementing Incrementing a method a It's almost as fast as calling a local variable in a function.

21. Incrementing a global variable is 2 times a local var. Incrementing a global variable is 2 times slower than incrementing a local variable.

22. Incrementing an object property (eg. $this- > prop++) is 3 times slower a local variable. Increment 1 object property (e.g. $this-) > prop++) is 3 times slower than incrementing a local variable.

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

24. Just declaring global variable without without using function a things down by about the amount a local var). PHP probably does a to see the global exists. Defining a local variable without calling it in a function also slows things down (by the same amount as incrementing a local variable). PHP will probably check to see if there are global variables.

25. Method invocation to independent independent
the the the class (before and after test method) with no change in performance) method calls seem to have nothing to do with the number of methods defined in the class, because I added 10 methods (before and after testing the methods), but there was no change in performance.

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

27. A function call one parameter and empty body body time doing localvar++ operations A similar method call is of course about 15 $localvar++ operations. The time it takes to call an empty function with one argument is equivalent to seven or eight local variable increments. A similar method call takes approximately 15 local variable increment operations.

28. Surrounding your string by 'es5531en of "will make a little little since for variables inside"..." but not inside '... '. Of course you only this this this this this you don t t don don t t the variables the the string '. Because PHP searches for variables in strings surrounded by double quotes, single quotes do not. Of course, you can only do this if you don't need to include variables in the string.

29. When echoing it faster faster faster them them instead dot Note This only works with echo, which is a function that strings strings as arguments. When multiple strings are output, it is faster to separate the strings with commas instead of periods. Note: only echo can do this. It is a "function" that takes multiple strings as arguments.

30. A PHP script will be least times times a by Apache Try to static HTML pages and fewer scripts.Apache parses 1 PHP script 2 to 10 times slower than parsing 1 static HTML page. Use more static HTML pages and less scripting.

31. Your PHP scripts recompiled every scripts scripts scripts to to by by by by to to by by by 25-100% by removing compile times. Unless the script can be cached, it will be recompiled 1 time per call. The introduction of an PHP caching mechanism typically improves performance by 25% to 100% to eliminate compilation overhead.

32. Cache as much as is memcached is high speed speed speed web applications by alleviating database load. OP code caches are useful so that to to to compiled every request. memcached.memcached is a high-performance in-memory object caching system that can be used to speed up dynamic Web applications and reduce database load. Caching the operation code (OP code) is useful so that the script does not have to be recompiled for every request.

33. When working with and to to to check that length you you d would want to use the () function.This function pretty since since operation operation any already already already known length of string in zval zval zval structure However because strlen() is a function is is function requires operations as lookup lookup followed by the execution of said function In instance you can improve speed using using an isset() trick. When you manipulate a string and need to verify that its length meets a certain requirement, you automatically use the strlen() function. This function performs fairly quickly because it does no calculations and only returns the known string length stored in the zval structure (C's built-in data structure for storing the PHP variable). However, because strlen() is a function, it is more or less slow, because the function call goes through a number of steps, such as lowercase, hash lookup, and then follows the function 1 being called. In some cases, you can use the isset() technique to speed up the execution of your code.

Ex.(examples below)

if (strlen ($foo) < 5) { echo "Foo is too short"; }

vs.(compare this with the following tips)

if (! isset($foo{5})) {echo "Foo is too short"; }

Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language does execution execution that execution execution have virtually no on of of the actual that that that that that that that This means that its execution does not require function lookups and lowercase letters. That is, you don't actually spend much overhead in the top-level code that verifies the length of the string.

34. When incrementing or decrementing of the variable $i++ happens to tad i This PHP specific and does not other languages, so don t your C thinking it ll become faster it won't. ++$i happens to be of causes in creation of temporary that that is is that is is is of the optimization that optimized Zend s PHP optimizer It is good idea keep in mind since not all opcode perform perform and there plenty plenty you you you you When you increment or decrement the variable $i, $i++ will be 1 slower than ++$i. This difference is unique to PHP and does not apply to other languages, so please do not modify your C or Java code and expect them to be instantly faster, it won't work. ++$i is faster because it requires only three instructions (opcodes) and $i++ requires four instructions. Postincrement actually produces a temporary variable that is then incremented. Preincrementing directly increments the original value. This is one type of optimization, as done by Zend's PHP optimizer. It's a good idea to keep this optimization in mind, because not all instruction optimizers do the same optimization, and there are a large number of Internet service providers (ISPs) and servers that do not have an instruction optimizer.

35. Not everything has to be OOP, often it too much much overhead, each method and object out of out out out out out out out out out out out out out out out out out

36. Do not implement every structure as as as a class, arrays are useful, too.

37. Don't split es111112en much, think, which code you really re-use.

38. You can always split code of method when needed, when needed.

39. Make use of the countless predefined functions.

40. If you very time consuming in in in your as C extensions if you have a lot of time consuming functions in your code, you may want to consider implementing them as an C extension.

41. Profile your code. A profiler shows you, which parts your consumes consumes many profiler overview. Evaluate and verify (profile) your code. The validator will tell you which parts of the code are consuming how much time. The Xdebug debugger includes validators, which generally show bottlenecks in the code.

42. mod_which is available as Apache module compresses data on the can the data to transfer up to 80%. mod_zip can be used as the Apache module to compress your data instantly and reduce the amount of data transferred by 80%.

Related articles: