Simply say PHP optimizer those things, PHP optimizer those things _php tutorial

Source: Internet
Author: User
Tags informix php compiler

Simply say PHP optimizer those things that PHP optimizes those things


When we write programs, we always want to make our programs take up the least amount of 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, usually can greatly improve the efficiency of

2. The integrity of the code is not compromised

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 speed up WEB 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, will not destroy the previous code

3, faster than the optimization code is much faster

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:

  

Ob_start (); Open buffer

?>

Full output of PHP page

  

$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.

In the following, we should pay attention to 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?


Someone can simply talk about the role of extension modules in PHP

When I first started out, I felt like I was downstairs, and I thought it would be a lot of 100-degree stuff to ask questions.
The result I really Baidu when found that Baidu was really bad to find.
However, it was finally found. The following table is a description and explanation of all extensions to the extension library.

-------------------------------------------Ornate Split Line------------------------------------------
Extension Library description annotations
Php_bz2.dll bzip2 compression Function Library No
Php_calendar.dll Calendar conversion function library from PHP 4.0.3 Built-in
Php_cpdf.dll ClibPDF Function Library None
Php_crack.dll password cracking function library None
Php_ctype.dll CType Family Function library from PHP 4.3.0 built-in
Php_curl.dll Curl, client URL library function library required: Libeay32.dll,ssleay32.dll (included)
Php_cybercash.dll Network cash payment function library PHP <= 4.2.0
The Php_db.dll DBM Library of functions has been deprecated. Replace with DBA (Php_dba.dll)
Php_dba.dll DBA: Database (dbm style) abstract layer function library None
Php_dbase.dll dBASE Function Library No
Php_dbx.dll DBX function Library
Php_domxml.dll DOM XML function library PHP <= 4.2.0 required: Libxml2.dll (included), PHP >= 4.3.0 required: Iconv.dll (included)
Php_dotnet.dll. NET function library PHP <= 4.1.1
The Php_exif.dll EXIF function library requires Php_mbstring.dll. And in php.ini, Php_exif.dll must be loaded after php_mbstring.dll.
Php_fbsql.dll frontbase function library PHP <= 4.2.0
Php_fdf.dll fdf: Form data Format function library required: Fdftk.dll (included)
Php_filepro.dll Filepro Function Library read-only access
Php_ftp.dll FTP function library from PHP 4.0.3 Built-in
Php_gd.dll GD library image function library is removed in PHP 4.3.2. Also note that the true color function cannot be used in GD1, instead of Php_gd2.dll.
Php_gd2.dll GD library image function library GD2
Php_gettext.dll gettext function library PHP <= 4.2.0 need Gnu_gettext.dll (included), PHP >= 4.2.3 need Libintl-1.dll,iconv.dll (included).
Php_hyperwave.dll Hyperwave Function Library None
Php_iconv.dll Iconv Character Set conversion required: Iconv-1.3.dll (included), PHP >=4.2.1 required Iconv.dll
Php_ifx.dll Informix Libraries required: Informix Library
Php_iisfunc.dll IIS management function library None ... Remaining full text >>

is PHP really that simple? Old people say PHP is very simple, how I bought a book to see the Chi began popping Chi

What language wants to be simple can be simple, think complex can be complex, the key to see how much you learn.
Say PHP Simple is the online, the real master is not to say PHP simple, the previous PHP does not support the object-oriented time, it is really only a small site, but from PHP5, PHP removed from the only small website hat, now still say that PHP simple can only be said to be a rookie, It's probably not PHP at all.
On the other hand, all the high-level languages are simple, because the high-level language design is to simplify the programmer's workload, PHP is no exception, this is the essence of object-oriented. So if a language is simple is actually a good thing, when you go the programmer will know.
As for the future of PHP, domestic with e-commerce, web2.0, as well as the so-called web3.0, and even cloud computing, have pushed PHP in the domestic hot atmosphere, coupled with the entire lamp platform low cost, high efficiency, is simply the domestic market environment of the perfect match.
I remind you once again that PHP is either a beginner or a novice in a different language, especially a beginner with a language that compares to each other.

http://www.bkjia.com/PHPjc/896043.html www.bkjia.com true http://www.bkjia.com/PHPjc/896043.html techarticle in brief, PHP optimization those things, PHP optimization of those things we are writing programs, always want to make their own programs occupy the minimum resources, run faster, less code. Often we are in ...

  • 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.