Simple Talk about PHP optimization those things

Source: Internet
Author: User
Tags execution functions html page sql php file php and php code php compiler

When we write programs, we always want to make our programs take the least amount of resources, run faster, and have less code. We often lose a lot of things while pursuing these. Now I want to talk about my understanding of PHP optimization. The goal of optimization is to get the fastest running speed and the easiest code to maintain with the least cost.

to do a wide range of optimizations, rather than die gnawing some program code

The optimization I'm talking about here is basically optimized from servers, Apache, databases, and not to your PHP code to improve the speed of your program, because instead of the regular optimizations in your program to string-handler functions to increase the speed of the program, The cost of optimization in a wide range is much smaller than this, and the rewards are much richer.

Optimization at a non-code place has the following benefits:

1, usually can greatly improve the efficiency

2, does not compromise the integrity of the Code

3. Able to quickly deploy

Caching Technology

The following is a common caching technique that can greatly improve efficiency through these caching techniques

When it comes to caching technology, we have to mention memcached, memcached is an efficient and fast distributed memory object caching system that is used to speed up WEB dynamic applications.

the principle of memcached

Memcached is run as a daemon on one or more servers, waiting for a connection from the receiving client, and the client can be written in a variety of languages (such as PHP). PHP and other clients in connection with the Memcached service, the next thing is to access objects, each accessed object has a unique identifier key, access operations through this key, saved to the Memcached object is actually placed in memory, is not stored in the cache file, which is why memcached can be so efficient and fast.

After memcached, let's say the common caching method

1, compile and opcode cache

Because PHP is an interpreted language, every PHP file needs to be compiled and executed at run time, with the same file, different user access, or the same user accessing the same file at different times, each time needing to recompile and run, which consumes a lot of time.

Compile-cache Each file is compiled only once after modification this reduces file IO operations, and the user accesses the machine instructions directly from the memory and executes them instead of reading them on the hard disk.

The most common PHP compiler cache tools are: Apc,accelerator,xcache

2, Global page caching –squid cache

Squid cache (squid for short) is a popular free software (GNU General Public License) proxy Server and Web caching server, squid as a Web server's predecessor cache server to cache related requests to improve the speed of the Web server.

3, the local cache of SQL cache

The main bottlenecks in most applications can often be traced back to the operations of the database, typically because of the large amount of time spent on complex database queries, and SQL caching can greatly reduce the load caused by complex queries.

Examples of SQL caching (using the memcached extension)

Code fragment:

$key = MD5 ("Some sort of SQL query");

if (!) ( $result = Memcache_get ($key))) {

$result = $pdo->query ($qry)->fetchall ();

Cached query Results one hour

Memcache_set ($key, $result, NULL, 3600);

}

4, the local cache code block cache

In order to optimize the PHP program, sometimes we have to optimize a piece of code to reduce a little bit of execution time, but compared to optimize the complexity of different PHP code snippets than the cache to directly ignore the code snippet optimization, the benefit is:

1, can quickly see the effect

2, will not break the previous code

3, speed is much faster than the optimization code

code block caching (also using the memcached extension)

Code fragment:

function complex_function_abc ($a, $b, $c) {

$key = __function__. Serialize

(Func_get_args ());

if (!) ( $result = Memcache_get ($key))) {

$result =//function code

Store execution results for 1 hours

Memcache_set ($key, $result, NULL, 3600);

}

return $result;

}

Of course, in addition to the above methods can also use the file cache (the data in the database to be stored in the file), but also generate static HTML files, etc., but the caching of these methods is still the file stored on the hard disk rather than in memory.

Output Control

In addition to the above caching technology, you can use output control to make programs execute less time

Here is the output control via PHP and Apache.

1. PHP Output Control

The main use here is the Ob_start () and the OB series functions in PHP, what can these functions do?

The first is static template technology. The so-called static template technology is in some way, so that users on the client side is generated by PHP HTML page. If the HTML page is not updated, then when another user browses to the page again, the program will no longer invoke PHP and the associated database, for some of the more informative sites, such as Sina,163,sohu. The benefits of this kind of technology are enormous.

code example:

<?php

Ob_start (); Open buffer

?>

All output of PHP page

<?php

$content = Ob_get_contents (); Get all the content of the PHP page output

$fp = fopen ("output.html", "w"); Create a file and open it, ready to write

Fwrite ($fp, $content); Write the contents of the PHP page to output.html, and then ...

Fclose ($FP);

?>

Of course there are many other uses for this OB series function that I am not here to explain.

2. Apache Output Control

Set the Sendbuffersize to the page size so that the page can be placed at once in the send buffer to increase processing speed.

Sendbuffersize directives

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 instruction sets the size (in bytes) of the TCP send buffer for the server. Increasing this value can result in two consequences: high speed and high latency (around 100ms). If set to 0″, the operating system defaults will be used.

Compiling your apache/php/database in source code allows your program to increase the speed of 10–15%

Here's what you should be aware of when optimizing code

1, short code is not equal to fast code

Many people want to write the code as concise as possible, but the shorter code sometimes takes longer to execute, so even with more code, it doesn't use slow code.

2, in the writing process should pay attention to the expansion of the program, rather than the pursuit of speed

3, before optimizing your code, look at the database-related parts, because most of the application bottlenecks in the database rather than the code

4, micro-optimization outweigh the gains

What is called micro-optimization? As mentioned earlier, replace the code in the regular Expression section with a string function instead. This has the following disadvantages:

(1) Take a long time

(2) will not solve your performance problems

(3) It is likely that the previous code will be corrupted to produce an unknown error

(4) Pay more than return

Here also have to mention a misunderstanding, some people in order to make the program more optimized, in the analysis of business logic will be optimized to consider in, so as to get better code and change the business logic. This is a very foolish idea, because the purpose of the procedure is to deal with the problems encountered in reality and to serve these problems.



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.