Home >Backend Development >PHP Tutorial >Summary of 5 php7 performance optimization tips

Summary of 5 php7 performance optimization tips

伊谢尔伦
伊谢尔伦Original
2017-06-27 10:01:222055browse

PHP7 has been released. As the largest version upgrade and the largest performance upgrade of PHP in 10 years, PHP7 has shown obvious performance improvements in multiple tests. However, in order for it to maximize its Performance, there are still a few things I would like to remind you.

1. Opcache

## Remember to enable Zend Opcache, because PHP7 is faster even if Opcache is not enabled than PHP-5.6 with Opcache enabled, so some people did not enable Opcache during the previous testing period. Enabling Opcache is very simple, configure it in php.ini

Add to file :

zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1"

2. Use a new compiler

Use a newer compiler, GCC 4.8 or above is recommended, because only GCC 4.8 or above PHP will enable Global Register for opline and execute_data support, which will bring about a 5% performance improvement (measured from the QPS perspective of Wordpres)

In fact, versions before GCC 4.8 also support it, but we found that it supports There is a bug, so it must be version 4.8 or above to enable this feature.

3. HugePage

I also introduced it in my previous article: Make your PHP7 more To quickly use Hugepage, first enable HugePages in the system, and then enable Opcache's huge_code_pages.

Take my CentOS 6.5 as an example, pass:

$sudo sysctl vm.nr_hugepages=512

Allocate 512 reserved large page memory:

$ cat /proc/meminfo | grep Huge
AnonHugePages: 106496 kB
HugePages_Total: 512
HugePages_Free: 504
HugePages_Rsvd: 27
HugePages_Surp: 0
Hugepagesize: 2048 kB

Then add in php.ini:

 opcache.huge_code_pages=1

In this way, PHP will allocate its own text segment and memory Huges in are stored in large memory pages to reduce TLB misses and improve performance.

4. Opcache file cache

Enable Opcache File Cache (experimental), by turning this on, we can let Opcache cache opcode

cache into an external file. For some scripts, there will be a significant performance improvement.In php.ini Add:

opcache.file_cache=/tmp

so that PHP will cache some Opcode binary export files in the /tmp directory, which can exist across the PHP

life cycle.

5. PGO

My previous article: Make your PHP7 faster (GCC PGO) also introduced that if your PHP is specifically for one project, such as just for your WordPress, or drupal, Or something else, then you can try to improve PHP through PGO, specifically to improve the performance of your project.

Specifically, take WordPress 4.1 as the optimization scenario. First, when compiling PHP:

$ make prof-gen

Then use your project to train PHP, for example, for WordPress:

$ sapi/cgi/php-cgi -T 100 /home/huixinchen/local/www/htdocs/wordpress/index.php >/dev/null

That is, let php-cgi run 100 times on the WordPress homepage to generate some profile information in the process.

Finally:

$ make prof-clean
$ make prof-use && make install

The PHP7 you compile at this time is the highest performance compiled version tailored for your project.

The above is the detailed content of Summary of 5 php7 performance optimization tips. For more information, please follow other related articles on the PHP Chinese website!

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