Home > Backend Development > PHP Tutorial > Briefly talk about PHP optimization things, PHP optimization things_PHP tutorial

Briefly talk about PHP optimization things, PHP optimization things_PHP tutorial

WBOY
Release: 2016-07-13 10:16:47
Original
783 people have browsed it

Simply talk about PHP optimization things, PHP optimization things

When we write programs, we always want to make our programs take up the least resources and run faster. Less code. Often we lose a lot of things while pursuing these. Next I want to talk about my understanding of PHP optimization. The purpose of optimization is to spend the least cost in exchange for the fastest running speed and the easiest to maintain code.

Carry out large-scale optimization instead of chewing on certain program codes

The optimization I am talking about here is basically optimization from the aspects of server, Apache, and database. It is not about improving your PHP code to increase the running speed of the program, because compared to how you program the program In terms of regular optimization in string processing functions to improve program speed, the cost of optimization in a large range is much smaller than this, but the reward is much richer.

Optimizing in non-code areas has the following benefits:

1. Under normal circumstances, it can greatly improve efficiency

2. Will not endanger the integrity of the code

3. Able to deploy quickly

 Caching technology

Let’s talk about commonly used caching technologies. These caching technologies can greatly improve efficiency

When talking about caching technology, I have to mention memcached. Memcached is an efficient and fast distributed memory object caching system, mainly used to accelerate WEB dynamic applications.

 Principle of Memcached

Memcached runs as a daemon in one or more servers, waiting to receive connection operations from clients. The client 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 objects. Each accessed object has a unique identifier key. Access operations are performed through this key and saved to memcached. The objects in are actually placed in memory, not stored in cache files, which is why memcached can be so efficient and fast.

After talking about memcached, let’s talk about the commonly used caching methods

 1. Compilation and OPCODE caching

Since PHP is an interpreted language, each PHP file needs to be compiled before execution. If the same file is accessed by different users, or the same user accesses the same file at different times, the same file will be accessed every time. It needs to be recompiled and then run, which takes a lot of time.

By compiling and caching, each file is only compiled once after modification, which reduces file IO operations. After the user accesses, the machine instructions are directly fetched from the memory and executed instead of being read from the hard disk.

The most common PHP compilation caching tools are: APC, Accelerator, xcache

 2. Global page cache – Squid Cache

Squid Cache (referred to as Squid) is a popular free software (GNU General Public License) proxy server and Web cache server. Squid serves as the front-end cache server of the web server to improve the speed of the Web server by caching related requests. .

 3. Local cache SQL cache

The main bottleneck in most applications can often be traced back to database operations, which are generally caused by complex database queries that consume a lot of time. SQL cache can greatly reduce the load caused by complex queries.

SQL cache example (using 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. Local cache code block cache

In order to optimize PHP programs, sometimes we have to optimize each code segment to reduce the execution time a little bit, but rather than optimizing complex and different PHP code segments, it is better to directly ignore these code segments through caching Optimization, the benefits of doing so are:

1. You can see the effect quickly

2. Will not destroy the previous code

3. It is much faster than optimizing code

Examples of 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 hour

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

 }

return $result;

 }

Of course, in addition to the above methods, you can also use file caching (retrieving data from the database and storing it in a file), and you can also generate static HTML files, etc. However, the caching of these methods still stores the files on the hard disk instead of in the memory. middle.

 Output control

In addition to the above caching technology, output control can also be used to reduce program execution time

Let’s talk about output control through PHP and APACHE

 1. PHP output control

The main uses here are 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 uses a certain method to enable users to get the html page generated by PHP on the client side. If this HTML page will no longer be updated, then 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, such as sina, 163, and sohu. The benefits of technology like this are huge.

Code example:

 

ob_start(); //Open buffer

 ?>

All output of the php page

 

 $content = ob_get_contents(); //Get all the contents output by the php page

 $fp = fopen(“output.html”, “w”); //Create a file and open it for writing

 fwrite($fp, $content); //Write all the contents of the php page to output.html, and then...

 fclose($fp);

 ?>

Of course, this ob series function has many other uses, which I won’t explain one by one here.

 2. apache output control

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

SendBufferSize command

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

Compiling your Apache/PHP/Database through source code can increase the speed of your program by 10–15%

Now let’s talk about what you should pay attention to when optimizing the code

1. Short code does not equal fast code

When writing a program, many people hope to write the code as concisely as possible, but shorter code sometimes requires longer execution time, so even if you use more code, do not use slow code

2. When writing a program, you should pay more attention to the scalability of the program instead of pursuing speed

3. Before optimizing your code, first look at the database-related parts, because the bottleneck of most applications is the database rather than the code

4. Micro-optimization does more harm than good

What is micro-optimization? As mentioned before, replace the regular expression part of the code with string functions. This has the following disadvantages:

 (1) It takes a long time

 (2) Will not solve your performance problem

 (3) It is very likely to destroy the previous code and produce unknown errors

(4) Pay more than you get in return

There is also a misunderstanding that I have to mention here. In order to make the program more optimized, some people take optimization into consideration when analyzing business logic, and thus change the business logic in order to get better code. This is a very stupid idea, because the purpose of the program is to solve problems encountered in reality and to serve these problems. How can it put the cart before the horse?

Can someone briefly talk about the role of extension modules in php

When I first started, I had the same idea as the person downstairs. I thought why should I ask questions about such a bunch of stuff?
As a result, when I tried Baidu, I discovered that Baidu was really hard to find.
However, I finally found it. The following table is the description, annotations and explanations of all extensions in the extension library.

---------------------------------------------GORGEOUS Dividing line---------------------------------------------
Extension library description Note
php_bz2.dll bzip2 compression function library None
php_calendar.dll Calendar conversion function library built-in since PHP 4.0.3
php_cpdf.dll ClibPDF function library None
php_crack.dll Password cracking function library None
php_ctype.dll The ctype family function library has built-in
php_curl.dll CURL since PHP 4.3.0. The client URL library function library requires: libeay32.dll, ssleay32.dll (included)
php_cybercash.dll Network cash payment function library PHP <= 4.2.0
php_db.dll DBM function library has been abandoned. Replace it with DBA (php_dba.dll)
php_dba.dll DBA: Database (dbm style) abstraction layer function library None
php_dbase.dll dBase function library None
php_dbx.dll dbx function library
php_domxml .dll DOM XML function library PHP <= 4.2.0 requires: libxml2.dll (included), PHP >= 4.3.0 requires: iconv.dll (included)
php_dotnet.dll .NET function library PHP <= 4.1.1
php_exif.dll The 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 formatting function library requires: fdftk.dll (included)
php_filepro.dll filePro function library read-only Visit
php_ftp.dll FTP function library built-in since PHP 4.0.3
php_gd.dll GD library image function library was deleted in PHP 4.3.2. Also note that true color functions cannot be used in GD1, use php_gd2.dll instead.
php_gd2.dll GD library image function library GD2
php_gettext.dll Gettext function library PHP <= 4.2.0 requires gnu_gettext.dll (included), PHP >= 4.2.3 requires libintl-1.dll , iconv.dll (included).
php_hyperwave.dll No HyperWave function library
php_iconv.dll ICONV character set conversion requires: iconv-1.3.dll (included), PHP >=4.2.1 requires iconv.dll
php_ifx.dll Informix Function library required: Informix library
php_iisfunc.dll IIS management function library None...The rest of the full text>>

Is PHP really that simple? Some people always say that PHP is very simple. Why did I buy this book and read it?

Any language can be as simple as you want or as complicated as you want. The key depends on how far you have learned it.
To say that PHP is simple is just what people on the Internet say. Real experts will not say that PHP is simple. In the past, when PHP did not support object-oriented, it was indeed only possible to build small websites. However, since PHP5, PHP has been removed. I have been told that I can only build small websites, but if I still say that PHP is simple, I can only say that I am a novice, and it is very likely that I don’t know PHP at all.
On the other hand, in fact, all high-level languages ​​are simple, because high-level languages ​​are originally designed to simplify the workload of programmers, and PHP is no exception. This is the essence of object-oriented. So if a language is simple, it is actually a good thing. Programmers will know it after you leave.
As for the future of PHP, domestic e-commerce, web2.0, so-called web3.0, and even cloud computing have promoted the popularity of PHP in the country. Coupled with the low cost and high performance of the entire LAMP platform, Efficiency is simply a perfect match for the domestic market environment.
I remind you again, anyone who says PHP is simple is either a rookie, or a rookie who knows another language, especially a rookie who compares languages ​​with each other.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/896043.htmlTechArticleSimply talk about PHP optimization things, PHP optimization things When we write programs, we always want to make ourselves The program occupies the smallest resources, runs faster, and has less code. Often we are...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template