How to optimize PHP program summary _php Skills

Source: Internet
Author: User
Tags memcached php script strlen switch case zend
1. If a method C++an be static, declare it static. Speed improvement is by a factor of 4. If a method is static, it can be statically declared. The rate can be raised to 4 times times.

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

3. Use echo ' s multiple parameters instead of string concatenation. Use Echo's multiple arguments to replace string concatenation with commas instead of periods.

4. Set the MaxValue for your for-loops before and isn't in the loop. Determine the maximum number of loops before executing the For loop, and do not compute the maximum value for Each loop.

5. Unset your variables to free memory, especially large. Unregister those unused variables, especially large arrays, to free up memory.

6. Avoid magic like __get, __set, __autoload avoid using __get,__set,__autoload as far as possible.

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

8. Use full paths in includes and requires, less time spent on resolving the OS paths. The full path is used when the file is included and less time is required to resolve the operating system path.

9. If you started executing, $_server[' request_time ' are preferred to time () if your To know when the script starts executing (that is, the server receives the client request), it is better to use $_server[' request_time ' than Time ().

If you can use STRNCASECMP, strpbrk and Stripos instead of regex. Check if the Strncasecmp,strpbrk,stripos function can be used instead of a regular expression to accomplish the same function.

Str_replace is faster than Preg_replace, but STRTR is faster than-str_replace by a factor of 4. The Str_replace function is faster than the Preg_replace function, but the STRTR function is four times times more efficient than the Str_replace function.

If the function, such as String replacement function, accepts both arrays and single characters as arguments, and If y Our argument list isn't too long, consider writing a few redundant replacement statements, passing one character at Tim E, instead of one line of the code that accepts arrays as search and replace arguments. If a string substitution function can accept an array or character as an argument, and the parameter length is not too long, consider writing an extra paragraph of substitution code so that each pass parameter is a character, rather than writing a single line of code to accept the array as a query and replacement parameter.

It ' s better to use SELECT statements than multi if, else if, statements. Using the Select Branch statement (the switch case) is better than using multiple if,else if statements.

The Error suppression with the @ is very slow. Using the @ Mask error message is very inefficient.

Turn on Apache Mod_deflate open Apache's mod_deflate module.

Close your database connections if you do with them. The database connection should be turned off when it is finished using.

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

Error messages are expensive. Error messages are costly.

Functions inside of For loop, such as for ($x =0; $x < count ($array); $x) the count () function gets Calle D each time. Try not to use functions in the For loop, such as for ($x =0; $x < count ($array); $x) call the count () function once per loop.

Incrementing a local variable the fastest. Nearly the same as calling a local variable in a function. Increments the local variable in the method, the speed is the fastest. Almost as fast as calling a local variable in a function.

Incrementing a global variable is 2 the slow than a local var. incrementing a global variable is twice times slower than incrementing one locally variable.

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

Incrementing an undefined the local variable are 9-10 times slower than a pre-initialized one. Incrementing an undefined local variable is 9 to 10 times times slower than incrementing a predefined local variable.

Just declaring a global variable without using it in a function, also slows things down (by about the same amount as in Crementing a local Var). PHP probably does a check to the if the global exists. Defining only one local variable, not calling it in a function, also slows down the speed (which is equivalent to incrementing a local variable). PHP will probably check to see if there are any global variables.

Method invocation appears to is independent of the number of methods defined in the class because I added meth ODS to the test class (before and then the test method) with no change in performance. The method call appears to be independent of the number of methods defined in the class, because I added 10 methods (both before and after the test method), but there was no change in performance.

Methods in derived classes run faster than ones in the base class. A method in a derived class runs faster than the same method defined in the base class.

A function call with one parameter and A empty function body takes about the same time as doing 7-8 $localvar + + Opera tions. A Similar method call is of course about $localvar + + operations. Calls an empty function with one parameter, which takes as much time as 7 to 8 increments of local variables. A similar method invocation takes approximately 15 times of local variable increments.

Surrounding your string by ' instead of ' 'll make things interpret a little faster since PHP looks for variables ins IDE "..." but not inside ' ... '. Of course you can only does this when you don ' t need to have variables in the string. It is quicker to use single quotes instead of double quotes to contain strings. Because PHP searches for variables in a string 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.

When echoing strings it's faster to separate them by comma of dot. Note:this only works with Echo, which are a function that can take several strings as arguments. When outputting multiple strings, it is faster to separate strings with commas instead of periods. Note: Only echo can do this, it is a "function" that can take multiple strings as arguments: ECHO is the language structure, not the real function, so the function is added double quotes.

PHP script would be served at the least 2-10 times slower than A static HTML page by Apache. Try to use the more static HTML pages and fewer scripts. Apache parses a PHP script 2 to 10 times times slower than parsing a static HTML page. Use static HTML pages as much as possible and use less scripting.

Your PHP scripts are recompiled every time unless the scripts are. Install a PHP caching product to typically increase performance by 25-100% by removing the times. Unless the script can be cached, it will be recompiled every time it is invoked. The introduction of a set of PHP caching mechanisms typically increases performance from 25% to 100% to exempt compilation overhead.

Cache as much as possible. Use memcached-memcached was a high-performance memory object caching system intended to speed up dynamic Web applications By alleviating database load. OP code caches are useful so, your script does not have to is compiled on every request. Try to do caching, you can use memcached.memcached is a high-performance memory object caching system, can be used to speed up dynamic Web applications, reduce the database load. It is useful to cache the operation Code (OP code) so that the script does not have to recompile for each request.

When working with strings and need to check this string is either of a certain length you ' d understandably wou LD want to use the strlen () function. This is pretty quick since it's operation does not perform any calculation but merely return the already known Le Ngth of a string available in the Zval structure (internal C struct used to store variables in PHP). However because Strlen () is a function it are still somewhat slow because the function call requires several operations suc H as Lowercase & Hashtable lookup followed by the execution of said function. In some instance can improve the speed of your code by using a Isset () trick. When you manipulate strings and need to verify that their lengths meet certain requirements, you will of course use the strlen () function. This function executes fairly quickly because it does not do any calculations and returns only the known string lengths stored in the Zval structure (c's built-in data structure for storing PHP variables). However, since strlen () is a function, it will be somewhat slower, because function calls go through a number of steps, such as lowercase letters, PHP is case-insensitive, and hash lookups follow the functions that are 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 with the techniques below)

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

Calling Isset () happens to being faster then strlen () because unlike (), strlen () is a isset language and not a construct tion meaning that it's execution does not require function lookups and lowercase. This means your have virtually no overhead on top of the actual code that determines the string ' s length. Calling Isset () happens to be faster than strlen (), because unlike the latter, Isset () as a language structure means that its execution does not require function lookup and lowercase. That is, you're actually not spending too much on the top-level code that verifies the length of the string.

  When incrementing or decrementing the value of the variable $i + + happens to be a tad slower then + + $i. This is something PHP specific and does don't apply to other languages, so don ' t go modifying your C or Java code thinking I T ' ll suddenly become faster, it won ' t. + + $i happens to is faster in PHP because instead of the 4 opcodes used for $i + + only need 3. Post incrementation actually causes in the creation of a temporary var this is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization which opcode optimized like Zend ' s PHP optimizer. It is still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are ISPs and servers running without an opcode optimizer. $i + + $i slower than + + when the execution variable $i is incremented or decremented. 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 quickly become useless. + + $i faster because it requires only 3 instructions (opcodes), $i + + requires 4 instructions. A post increment actually produces a temporary variable, which is then incremented. And the predecessor increment increases directly on the original value. This is one of the most optimized processes, as Zend's PHP optimizer has done. Keeping this optimization in mind is a good idea, because not allCommand Optimizer will do the same optimization, and there is a large number of Internet service providers (ISPs) and servers that do not have assembly instruction optimizer.

Not everything has to being OOP, often it is too so much overhead that each method and object call consumes a lot of memory. It's not something. object-oriented (OOP), object-oriented often expensive, each method and object invocation consumes a lot of memory.

Do not implement every data structure as a class, arrays are useful, too. Arrays are also useful not to implement all the data structures with classes.

Don ' t split methods too much, and the which code you'll really re-use. Don't subdivide the method too much and think carefully about what code you really intend to reuse.

You can always split the code of "A" later, when needed. When you need it, you can always break the code into methods.

Make-use of the countless predefined functions. Try to use a lot of PHP built-in functions.

If you are have very time consuming functions in your code, consider writing as C them. If there are a lot of time-consuming functions in your code, you might consider implementing them in C-extension mode.

Profile your code. A Profiler shows, which parts of your code consumes how to many time. The Xdebug debugger already contains a profiler. Profiling shows you to the bottlenecks in overview. Evaluate your code for the test (profile). The inspector will tell you which parts of the code are consuming much of the time. The Xdebug debugger includes a test procedure that can be used to show code bottlenecks in general.

Mod_gzip which is available as a Apache module compresses your data on the fly and can reduce the data to transfer up To 80%. Mod_zip can be used as an Apache module to instantly compress your data and reduce data transfer by up to 80%.
Related Article

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.