Next I want to explain my understanding of PHP optimization. The goal of optimization is to get the fastest running speed and the most easily maintained code at the lowest cost.
Next I want to explain my understanding of PHP optimization. The goal of optimization is to get the fastest running speed and the most easily maintained code at the lowest cost.
When writing a program, we always want to minimize the resource occupation of our program, run faster, and run less code. We often lose a lot of things while pursuing these things. Next I want to explain my understanding of PHP optimization. The goal of optimization is to get the fastest running speed and the most easily maintained code at the lowest cost.
Optimize the program code in a wide range, instead of killing some program code.
The optimization I mentioned here is basically optimization from the server, Apache, and database aspects, rather than improving your PHP code to speed up program running, because, compared to optimizing the regular expression in the program as a string processing function to speed up the program, the optimization in a large scope requires much less cost than this, however, it is much richer to be paid.
Optimization without Code has the following benefits:
1. Generally, the efficiency can be greatly improved.
2. The code integrity will not be compromised.
3. Fast deployment
Cache Technology
The common cache technologies can greatly improve the efficiency.
When talking about caching technology, we have to mention memcached. memcached is an efficient and fast distributed memory object cache system, mainly used to accelerate Dynamic WEB applications.
Principle of Memcached
Memcached runs on one or more servers in the form of a daemon, waiting for receiving client connection operations. The client can be written in various languages (such as PHP ). After the PHP client establishes a connection with the memcached service, the next thing is to access the object. Each accessed object has a unique identifier key, and the access operation is performed through this key, objects saved to memcached are actually stored in the memory, not in the cache file. This is why memcached is so efficient and fast.
After memcached is completed, the common cache methods are described below.
1. Compilation and OPCODE caching
Because PHP is an interpreted language, every PHP file needs to be compiled before execution during runtime. Different users access the same file, or the same user needs to re-compile and run each time to access the same file at different times, which consumes a lot of time.
By compiling and caching, each file is compiled only once after modification, which reduces the file I/O operations. After the user accesses the file, the machine commands are directly extracted from the memory and executed instead of read from the hard disk.
The most common PHP compilation and cache tools are APC, Accelerator, and xcache.
2. Global page Cache-Squid Cache
Squid Cache (Squid for short) is a popular free software (GNU General Public License) proxy server and Web Cache Server, as the front cache server of the Web server, Squid caches related requests to speed up the Web server.
3. Local cache SQL Cache
The major bottlenecks in most applications can often be traced back to database operations. Generally, complicated database queries consume a lot of time, the SQL cache can greatly reduce the load caused by complex queries.
SQL cache example (memcached extension is used)
Code snippet:
$ Key = md5 ("some sort of SQL query"); if (! ($ Result = memcache_get ($ key) {$ result = $ pdo-> query ($ qry)-> fetchAll (); // cache the query result for one hour memcache_set ($ key, $ result, NULL, 3600 );}
4. code block caching in local cache
To optimize the PHP program, sometimes we have to optimize code segments to reduce the execution time, however, it is better to ignore the optimization of different PHP code segments through caching than to optimize complicated ones. The advantages of doing so are:
1. You can quickly see the results
2. the previous Code will not be damaged.
3. The speed is much faster than the optimization code
Columns cached by code blocks (memcached extensions are also used)
Function complex_function_abc ($ a, $ B, $ c) {$ key = _ FUNCTION _. serialize (func_get_args (); if (! ($ Result = memcache_get ($ key) {$ result = // Function Code // Save the execution result for 1 hour memcache_set ($ key, $ result, NULL, 3600 );} return $ result ;}
Of course, in addition to the above methods, you can also use file caching (retrieving and storing data from the database in files) and generate static HTML files, however, the cache of these methods still stores files on the hard disk rather than in the memory.
Output Control
In addition to the above caching technology, you can also use output control to reduce the execution time of programs.
The following uses PHP and APACHE to describe output control.
1. PHP Output Control
Here we mainly use ob_start () and OB series functions in PHP. What can these functions do?
The first is static template technology. The so-called static template technology is to make the user obtain the html page generated by PHP on the client side in some way. If this html page is no longer updated, when another user browses this page again, the program will no longer call PHP and related databases. For some websites with a large amount of information, for example, sina, 163, sohu. The benefits of similar technologies are enormous.
Sample Code:
Sample Code:
The Code is as follows:
<? Php
Ob_start (); // open the buffer
?>
All php page output
The Code is as follows:
<? Php
$ Content = ob_get_contents (); // Retrieves all the content output on the php page.
$ Fp = fopen(export output.html "," w "); // create a file, open it, and prepare to write
Fwrite ($ fp, $ content); // write the content of the PHP page into output.html, and then ......
Fclose ($ fp );
?>
Of course, this ob Series function has many other functions that I will not explain here.
2. apache Output Control
Set SendBufferSize to the page size, so that the page can be placed in the sending buffer at one time to increase the processing speed.
SendBufferSize command
Description: size of the TCP sending buffer (in bytes)
Syntax: SendBufferSize bytes
Default Value: SendBufferSize 0
Scope: server config
Status: MPM
Modules: beos, mpm_netware, mpm_winnt, mpmt_os2, prefork, and worker
This command sets the TCP sending buffer size (in bytes) of the server ). Increasing this value will lead to two consequences: high speed and high latent time (about ms ). If it is set to "0", the default Operating System value is used.
Compiling your Apache/PHP/Database through source code can increase the speed of your program by 10-15%
We should pay attention to the following during code optimization:
1. short code is not equal to quick code
Many people want to write the code more concisely and better, but the shorter the code, the longer the execution time, so even more code is used, no slow code is used.
2. When writing a program, you should pay more attention to program scalability rather than speed.
3. before optimizing your code, you should first look at the database-related part, because the bottleneck of most applications lies in the database rather than the code.
4. Micro-optimization is not worth the candle
What is microoptimization? As mentioned above, the code in the regular expression part is replaced by a string function. This method has the following Disadvantages:
(1) It takes a long time
(2) won't solve your performance problems
(3) It is very likely that the previous Code will be damaged to generate unknown errors.
(4) pay is greater than return