Pursuing program speed rather than programming speed

Source: Internet
Author: User
During website construction, the program running speed and website download speed are important factors that affect the success or failure of the website. As a Web programmer, you should pay more attention to the code running speed. The methods described below increase the code running speed to varying degrees. 1. use embedded HTML code instead of PHP echo statements.
Because PHP is an embedded Web programming language, HTML code and PHP code can be embedded into each other. However, many programmers are worried that the PHP interpreter will be called multiple times when "" is used too much in HTML code to reduce the running speed of PHP code, therefore, we would rather use PHP echo statements to output HTML code rather than directly using HTML code. But the opposite is true. Every PHP page only calls the PHP interpreter once to explain all the PHP code. Therefore, the PHP code is embedded only when necessary, and most of the time the HTML code is used to input the result directly, this will not only reduce the program running speed, but also reduce the resolution of echo statements, which can often improve the code running speed.
The following code proves our conclusion. In this code, we used the time test function described above.

2. use str-replace instead of ereg-replace
Programmers who are familiar with Perl programming are more willing to use ereg_replace to complete string replacement, because the usage of ereg_replace in PHP is similar to that of pattern matching in Perl. However, the following code proves that replacing ereg_replace with str_replace can greatly improve the code running speed.

Test the running speed of str_replace and ereg_replace

// This code tests the running speed of str_replace.


Emphasis;?>

For ($ I = 0; I I <1000; $ I ++ ){
Str_replace (I>, B>, $ string ).

}
?>

// This code tests the running speed of ereg_replace.

For ($ I = 0; I I <1000; $ I ++ ){
Ereg_replace (<([/] *) I >,< \ 1b>, $ string ).

}
?>

// Print the result

Conclusion

Time when str_replace is used-

Time when ereg_pattern is used-
Run the above code and the result is:
Time when str_replace is used-0.089757
Time when ereg_pattern is used-0.248881
From the running results, we can see that using str_replace to replace ereg_replace as a string replacement function greatly improves the code running speed.


3. pay attention to string references.
Like many other programming languages, PHP can use double quotation marks ("") to reference strings, or single quotation marks (). However, in PHP, if double quotation marks are used to reference a string, the PHP parser will first analyze whether there is any reference to the variable in the string. if there is a variable, the variable will be replaced. If it is a single quotation mark, it is not so complex-directly display all strings contained in single quotation marks. Obviously, using single quotes to reference string variables in PHP programming is faster than using double quotes.


4. avoid joint operations in the database
Compared with other Web programming languages, PHP databases are very powerful. However, running a database in PHP is still a very time-consuming and laborious task. Therefore, as a Web programmer, we should minimize the number of database queries, at the same time, appropriate indexes should be created for the database. Another thing worth noting is that when using PHP to operate a database, try not to use the union operation of multiple data tables. even though the union operation can enhance the database query function, but it greatly increases the burden on the server.
To illustrate this problem, let's take a look at the simple example below.


Two data tables foo and big_foo are created in the database. In the data table foo, there is only one field that contains all the natural numbers from 1. The data table big_foo also has only one field, but contains all the natural numbers from 1 to 1,000,000. Therefore, in terms of size, big_foo is equal to foo and it performs union operations.
$ Db-> query ("select * from foo ");
Secs 0.032273
$ Db-> next_record ();
Secs 0.00048999999999999
$ Db-> query ("insert into foo values (NULL )");
Secs 0.019506
$ Db-> query ("select * from foo as a, foo as B ");
Secs 17.280596
$ Db-> query ("select * from foo as a, foo as B where a. id> B. id ");
Secs 14.645251
$ Db-> query ("select * from foo as a, foo as B where a. id = B. id ");
Secs 0.041269
$ Db-> query ("select * from big_foo ");
Secs 25.393672
From the above operation results, we can find that the speed of joining two data tables with 1000 records is not much faster than that of a large data table with 1000000 records.


5. Note the difference between include and require.
In PHP, the include () and require () functions are the same, but there are some differences in usage. include () is a conditional inclusion function, while require () it is an unconditional include function. For example, in the following example, if the variable $ somgthing is true, it will contain the file somefile:
If ($ something ){
Include ("somefile ");
}
However, no matter what value $ something takes, the following code will include the file somefile into the file:
If ($ something ){
Require ("somefile ");
}
The following interesting example fully demonstrates the differences between the two functions.
$ I = 1;
While ($ I <3 ){
Require ("somefile. $ I ");
$ I ++;
}
In this code, each loop contains the same file. Obviously, this is not the original intention of the programmer. from the code, we can see that this code will include different files in each loop. To complete this function, you must seek help from the function.

Include (){
$ I = 1;
While ($ I <3 ){
Include ("somefile. $ I ");
$ I ++;
}


6. Note the difference between echo and print.
The echo and print functions in PHP are basically the same, but there are also slight differences between the two. Print can be used as a common function in PHP code. for example, after executing the following code, the value of the variable $ res will be 1.
$ Ret = print "Hello World ";
This means that print is available in some complex expressions, but echo is not. Similarly, the echo statement runs slightly faster than the print statement in the code, because the echo statement does not require any value to be returned.

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.