Home  >  Article  >  Backend Development  >  PHP language-level optimization and code optimization_PHP tutorial

PHP language-level optimization and code optimization_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:36:13749browse

In large-scale system development, Cache is undoubtedly crucial. In the PHP world, although there are not as many cache solutions to choose from as in Java, there are still some mature solutions, starting from I learned the following from "advanced PHP programming":

1. Language-level optimization: PHP has many engine-level APIs. Through these APIs, the behavior of engine execution can be changed to achieve the purpose of optimizing operation. Among them, the most worthwhile thing to do is to cache the compilation results. As we all know, every time PHP is executed, it needs to go through the process of source code -> compilation -> intermediate code -> engine execution. For some large applications, a considerable amount of time is spent on useless compilation (not just the PHP that accesses the page) Files need to go through the compilation process. When require() and include() are used in the script, some files included need to be compiled). By caching the compilation results, the performance of the system can be greatly improved (proportional to the complexity and scale of the system).

The three mainstream tools in the PHP world that can implement compiled cache are:

The Zend Accelerator—A commercial, closed-source, for-cost compiler cache produced by Zend Industries
The ionCube Accelerator—A commercial, closed-source, but free compiler cache written by Nick Lindridge and distributed by his company , ionCube
APC—A free and open-source compiler cache written by Daniel Cowgill and George Schlossnagle

The installation method of APC, APC is included in PECL, the specific installation is as follows 190-823 190-802:

Run command

#pear install apc

After

, add in the php.ini file:

extension = /path/to/apc.so

In this way, the installation is completed. When you run php next time, APC will be automatically activated and the compilation results will be cached in the shared memory. The next time you execute it, the editing results will be directly obtained from the memory and executed. No need Compile again.

Question: Can APC automatically recompile PHP that has changed since the last compilation?

2. PHP code optimization: Use some tools to generate high-quality intermediate code after compilation, as follows:

The Zend Optimizer is a closed-source but freely available optimizer.
The ionCube accelerator contains an integrated optimizer.
proof-of-concept optimizer in PEAR.

Main functions of the optimizer:

1. Eliminate useless code, such as dead code that will never be executed.

2. Calculation of constants, for example, changing $seconds_in_day = 24*60*60 directly to $seconds_in_day = 86400;

3. Other code optimization functions, such as this statement:

$count++;

will be optimized to ++$count, making execution faster. Of course, if the statement is $i = $count++; there will be no optimization

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508244.htmlTechArticleIn large-scale system development, Cache is undoubtedly crucial. In the PHP world, although it is not as good as in Java, There are so many cache solutions to choose from, but there are still some mature...
Statement:
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