If you improve the performance of your PHP program

Source: Internet
Author: User
Tags case statement switch case

A generation of great God Hoare once said: "Premature optimization is the root of all misfortune." "When I was a rookie, I did not know whether this sentence is correct, but when I became an old bird, I found that this sentence is quite sharp, said the right, also deserves it to spread so widely."

To optimize the performance of PHP, the 1th is to pay attention to the cache, someone said that if PHP uses the cache, it will improve performance 500 times times, unfortunately Zend Company is committed to selling their products, so a company put a language to the pit, not to mention the company's problems, in terms of the importance of caching, Must not be overlooked. We can use a cache module (such as memcache) or cache template (Smarty) and so on for a series of cache processing, of course, the two pages I described earlier static and file caching of database information is also in this category.

The 2nd is the memory consumption problem, this is still relatively large, said a few details, that is, some people like to assign a variable to a copy, thereby increasing memory overhead, such as we receive from get parameters, direct use on it, not to be assigned to another variable, resulting in the variable memory consumption doubled, For example, the following code:

<?php//is just for the beautiful new variable $msg = strip_tags ($_get[' msg "); Echo $msg;
In fact, directly with the following code is possible:

echo strip_tags ($_get[' msg ");
The 3rd is the drawbacks of other languages, such as Java programmers who might like to write get functions and set functions, but as PHP programmers do not have to do this at all, directly manipulate properties, because using these attributes, in addition to increasing overhead, does not do any meaningful work.

4th is the operation of the database, some novice like to use a data to check once, in fact, every time we connect the database and then disconnect the cost is pretty big, we should do the SQL operations together, unified execution, to avoid multiple database connection and disconnection.

5th is in the include or require file, the use of absolute path is definitely much faster, in addition you can use the automatic loading mechanism, that is convenient, but also to ensure the efficiency, about the automatic loading, you can see my blog, just wrote today.

6th is in some big parts of the tuning, many people like to use smarty, if it is a large site, but also need to consider the cost of smarty, some statistics, Smarty will occupy 10% of the cost, this is still very considerable.

The 7th is to use single quotation marks instead of double quotation marks to define the string to make it a little faster, because PHP will search for variables in a string surrounded by double quotes, but not single quotes. Using a static method also increases speed because it avoids instantiation of the class and saves overhead. When using echo to output multiple character information, it is faster to use commas instead of the origin because it eliminates the overhead of stitching strings. An include file will also reduce overhead by using an absolute path, because it avoids PHP's ability to find files in Includ_path and improve performance.

The 8th is that for simple strings, do not use regular expressions, which increases overhead and uses the switch case statement more than if. ElseIf is optimized for many, because it eliminates the overhead of multiple judgments. For variable operations, incrementing a local variable is the fastest, incrementing a global variable by about twice times slower, incrementing an object's properties by about three times times slower.

The 9th is a bit of a reason for our static, because Apache parsing a PHP script is about twice times slower than parsing a static HTML page, so this is one of the advantages of static. When we read the file, we can use it with file_get_contents instead of Fopen,fread.

The last point is that the efficiency of the foreach is higher than for and while efficiency, is not in the mind of what else, if the reader can think of, want to add Austria, yes, and APC this cache, in fact, I said the 1th is the cache ...


If you improve the performance of your PHP program

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.