To elaborate on the PHP optimizations.

Source: Internet
Author: User
Tags php compiler

When we are programming in PHP, we always want to make our programs take up the least resources, run faster and have less code. We tend to lose a lot of things while pursuing them. Next I would like to talk about my understanding of PHP optimization. The goal of optimization is to get the fastest running speed and the easiest to maintain code with the least cost.

Do a wide range of optimizations, rather than die chewing some program code

The optimizations I'm talking about here are basically optimizations made from servers, Apache, and databases, rather than improving your PHP code to improve the speed of your program, because you can improve program speed by optimizing your program's regularization to string processing functions. The cost of optimization in a wide range is much smaller than this, and the rewards are much more lucrative.

Optimizations at non-code places have the following benefits:

1. Generally, the efficiency can be greatly improved .

2. does not compromise the integrity of the Code

3. ability to deploy quickly

Caching technology

The following is a common cache technology, through these caching technology can greatly improve efficiency

When it comes to caching, we have to mention memcached, memcached is an efficient and fast distributed memory object caching system that is mainly used to accelerate web development of dynamic applications.

The principle of memcached

Memcached is a daemon that runs on one or more servers, waits for a connection operation from the receiving client, and the client can be written in various languages (for example, PHP). PHP and other clients after establishing a connection with the Memcached service, the next thing is to access the object, each access to the object has a unique identifier key, access operations are through this key, the object saved to memcached is actually placed in memory, is not saved in the cache file, which is why memcached can be so efficient and fast.

After memcached, let's say the usual caching methods

1. compilation and opcode cache

Because PHP is an interpreted language, every PHP file needs to be compiled and executed at run time, with a single 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.

By compiling the cache each file is compiled only once after the modification so that the file IO operation is reduced, and the user accesses the machine instructions directly from the memory and executes instead of the hard disk read out.

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

2. Global Page Caching –squid cache

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

3, local cache of SQL cache

The major bottlenecks in most applications can often be traced back to the operations of the database, typically because of complex database queries that take a lot of time, while SQL caching can greatly reduce the load caused by complex queries.

Example of SQL cache (memcached extension used)

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 for local cache

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

1) can quickly see the effect

2) 2, does not destroy the previous code

3) speed is much faster than optimizing code

code block caching (also using the memcached extension)

Code snippet:

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 method can also use the file cache (the data in the database to be stored in the file), but also to generate static HTML files, etc., but the cache of these methods will be stored on the hard disk instead of memory.

Output control

In addition to the above caching technology, output control can be used to make the program execute less time

Below through PHP and Apache say output control

1, PHP output control

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

The first is the 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 this HTML page is not updated again, 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 a technology like this are enormous.

code example:

<?php

Ob_start (); Open buffer

?>

Full output of PHP page

<?php

$content = Ob_get_contents (); Get the full contents 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, then ...

Fclose ($FP);

?>

Of course, this OB series function has a lot of other uses I'm not here to explain.

2. Apache Output Control

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

sendbuffersize directive

Description: TCP Send buffer size (bytes)

Syntax: sendbuffersize bytes

Default value: Sendbuffersize 0

Scope: Server Config

Status: MPM

Module: BeOS, Mpm_netware, Mpm_winnt, Mpmt_os2, prefork, worker

This instruction sets the size (in bytes) of the TCP send buffer for the server. Raising this value results in two consequences: high speed and high latent time (around 100ms). If set to 0″, the operating system default value will be used.

Compiling your apache/php/database by source code allows your program to increase 10–15% speed.

Let's say that you should be aware of the code optimization:

1, short code is not equal to fast code

Many people want to write a program as concise as possible, but the shorter the code sometimes takes a longer time to execute, so even more code does not use the slower code

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

3. Before you optimize your code, take a look at the database-related sections, because most application bottlenecks are in the database rather than the code

4, micro-optimization outweigh the gains

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

(1) take a long time

(2) will not solve your performance problems

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

(4) pay more than return

There is also a misconception that some people, in order to make the program more optimized, consider the optimization in the analysis of the business logic, in order 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 the real world and to serve these problems, how can we get the cart before the horse?

To elaborate on the PHP optimizations.

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.