PHP language optimization and code optimization-PHP Tutorial

Source: Internet
Author: User
Tags apc ioncube
PHP language optimization and code optimization. In large-scale system development, Cache is undoubtedly crucial. in the php world, although there are not as many cache solutions as in java, there are still some mature

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

1. language-level optimization: php has a number of engine-level APIs. these APIs can change the engine execution behavior to optimize the operation. Among them, the best thing to do is to cache the compilation result. As we all know, php needs to go through the source code-> compile-> intermediate code-> engine to execute such a process every time it is executed. for some large applications, A considerable amount of time is spent on compilation. (not only does the php file accessing the page need to be compiled, but when the script uses require (), include () including some files must be compiled ). By caching the compilation results, the system performance can be greatly improved (directly proportional to the complexity and scale of the system ).

Three mainstream php Tools in the world that can compile cache are:

The Zend Accelerator-A commercial, closed-source, for-cost compiler cache produced by Zend Industries
The ionCube Accelerator-A specified CIAL, 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 is included in PECL. the installation method is as follows: 190-823 190-802:

Run commands

# Pear install apc

Then, add the following in the php. ini file:

Extension =/path/to/apc. so

In this way, the installation is complete. When php is run next time, APC will be automatically activated and the compilation result will be cached in the shared memory, the next execution will directly obtain the editing result from the memory for execution without re-compiling.

Q: Can APC automatically recompile php changed after the last compilation?

2. php code optimization: Some tools can be used to compile and generate high-quality intermediate code, as shown below:

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 features of the Optimizer:

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

2. constant calculation. for example, change $ seconds_in_day = 24*60*60 to $ seconds_in_day = 86400;

3. other code optimization functions, such as the following statement:

$ Count ++;

It is optimized to ++ $ count to make the execution faster. Of course, if the statement is $ I = $ count ++, no optimization will be performed.

...

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.