In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the optimization of PHP code". In the daily operation, I believe that many people have doubts about the optimization of PHP code. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "the optimization of PHP code". Next, please follow the editor to study!
When we write programs, we always want to make our programs take up the least resources, run faster, and have less code. Often we lose a lot of things while pursuing these things. Next I would like to talk about my understanding of PHP optimization. The purpose of optimization is to get the fastest running speed and the most maintainable code at the least cost.
Do a wide range of optimization instead of gnawing at some program code
The optimization I am talking about here is basically from the server, Apache, database and other aspects of the optimization, not to improve your PHP code to improve the speed of the program, because compared to you optimize the regularities in the program into string processing functions to improve the speed of the program, the cost of optimization on a large scale is much smaller than this, but the reward is much more generous.
Optimization in non-code has the following benefits:
1. Under normal circumstances, efficiency can be greatly improved.
2. it will not endanger the integrity of the code.
3. Ability to deploy quickly
Caching technology
Let's talk about the common caching techniques, through which the efficiency can be greatly improved.
When talking about caching technology, we have to mention memcached. Memcached is an efficient and fast distributed in-memory object caching system, which is mainly used to accelerate WEB dynamic applications.
The principle of Memcached
Memcached runs as a daemon on one or more servers, waiting to receive connection operations from the client, which can be written in various languages (such as PHP). After clients such as PHP establish 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 carried out through this key. The object saved in memcached is actually placed in memory, not in the cache file, which is why memcached can be so efficient and fast.
After talking about memcached, let's talk about the common caching methods.
1. Compilation and OPCODE caching
Because PHP is an interpreted language, every PHP file needs to be compiled and executed at run time, the same file, different users access the same file, or the same user accesses the same file at different times, and needs to be recompiled and run each time, which takes a lot of time.
By compiling and caching each file is compiled only once after modification, which reduces the file IO operation. After user access, the machine instructions are directly fetched from memory and executed instead of read from the hard disk.
The most common PHP compilation cache workers have: APC,Accelerator,xcache
2. Global page cache-Squid Cache
Squid Cache (abbreviated as Squid) is a popular free software (GNU General Public license) proxy server and Web cache server. Squid as the front cache server of the web server improves the speed of the Web server by caching related requests.
3. SQL cache of local cache
In most applications, the main bottleneck can be traced back to the operation of the database, generally because of complex database queries and spend a lot of time, and SQL cache can greatly reduce the load caused by complex queries.
Example of SQL caching (using the memcached extension)
Code snippet:
$key = md5 ("some sort of sql query"); if (! ($result = memcache_get ($key)) {$result = $pdo- > query ($qry)-> fetchAll (); / / Cache query results for one hour memcache_set ($key, $result, NULL, 3600);}
4. Code block cache of local cache
In order to optimize the PHP program, sometimes we have to optimize the code snippets to reduce the execution time a little bit, but it is better to ignore the optimization of these code snippets directly through caching than to optimize complex and different PHP code snippets. The benefits of this are:
1. You can see the effect very quickly.
2. Will not break the previous code
3. The speed is much faster than optimizing the code
Column children of the code block cache (also using the memcached extension)
Function complex_function_abc ($a, $b, $c) {$key = _ _ FUNCTION__. Serialize (func_get_args ()); if (! ($result = memcache_get ($key) {$result = / / function code / / store 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 (take out the data from the database and store it in a file), and generate static HTML files, etc., but the cache of these methods still stores the files on the hard disk rather than in memory.
Output control
In addition to the above caching techniques, you can also use output control to make the program execute less time.
Let's talk about output control through PHP and APACHE.
1. PHP output control
The main use here is ob_start () and the OB series of functions in PHP. What can these functions do?
The first is static template technology. The so-called static template technology is to make users get the html pages generated by PHP on the client side in some way. If the html page is no longer updated, then when another user visits the page again, the program will no longer call PHP and related databases, for some informative sites, such as sina,163,sohu. The benefits of technology like this are enormous.
Code example:
Code example:
The copy code is as follows:
Full output of the php page
The copy code is as follows:
Of course, there are many other uses of this ob series of functions. I won't explain them here.
2. Apache output control
Set the SendBufferSize to the page size so that the page can be placed in the send buffer at once to increase processing speed.
SendBufferSize instruction
Description: TCP send buffer size (bytes)
Syntax: SendBufferSize bytes
Default value: SendBufferSize 0
Scope: server config
Status: MPM
Modules: beos, mpm_netware, mpm_winnt, mpmt_os2, prefork, worker
This directive sets the size (in bytes) of the server's TCP send buffer. Increasing this value has two consequences: high speed and high latency (around 100ms). If set to "0", the operating system default value is used.
Compiling your Apache/PHP/Database through source code can increase your program speed by 10-15%
Let's talk about what we should pay attention to when optimizing the code.
1. Short code is not equal to fast code
Many people want to write code as succinctly as possible, but shorter code sometimes takes longer to execute, so even if you use more code, you don't use slow code.
2. When writing a program, we should pay more attention to the expansibility of the program rather than the pursuit of speed.
3. Before you optimize your code, look at the parts related to the database, because the bottleneck of most applications lies in the database rather than the code
4. The loss of micro-optimization outweighs the gain.
What is micro-optimization? As mentioned earlier, replace the regular expression part of the code with a string function. This has the following disadvantages:
(1) it takes a long time
(2) it won't solve your performance problem.
(3) it is likely to destroy the previous code and cause unknown errors.
(4) giving is greater than return.
There is also a misunderstanding that some people take optimization into account when analyzing business logic in order to make the program more optimized, thus changing the business logic in order to get better code. This is a very stupid idea because the purpose of the program is to deal with the problems encountered in reality and to serve these problems, so how can we put the cart before the horse?
At this point, the study of "how to optimize PHP code" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.