PHP 5到PHP 7性能全评测(含未发布的JIT版PHP 8对比)

2019-05-2721:48:54后端程序开发Comments3,727 views字数 4335阅读模式

PHP 在核心和附加扩展开发的功能得到了蓬勃发展,如网络通信,解析,缓存和数据库支持。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

语言本身也在发展,带来了一系列的改进。这包括支持面向对象的结构,例如类,接口, traits,闭包等。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

对于许多开发人员来说,仅有新功能是不够的。随着语言越来越受欢迎, PHP 社区对于提供更好性能,可扩展性和更少内存使用的需求越来越强烈文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 开发团队近 20 年来一直致力于解决这些需求,虽然 PHP 3 的引入大大提高了性能,但直到 Andi Gutmans 和 Zeev Suraski 引入 Zend Engine 并发布 PHP 4, PHP 的性能才开始变得正式起来。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

2000 年推出的新的内存编译器和执行器模型大大提高了 PHP 的性能(提高了 5 倍甚至 10 倍),并首次被正式的 Web 应用程序和站点所使用。我们可以说,今天 PHP 的成果远远超出了任何人在 PHP 项目诞生时的期望。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 的蓬勃发展增加了改善性能的欲望。幸运的是, Zend Engine 中设计的模型为持续优化性能提供了良好的基础。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

虽然 PHP 没有带来实质性的性能提升,并且在某些情况下甚至比 PHP4 更慢,一个由 Dmitry Stogov 领导的团队在社区的大力帮助下已经在后续版本中不断优化语言,在 PHP 5.6 发布的时候,在大多数情况下,性能提升在 和 3x 之间。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

2015 年 12 月, PHP 7.0 取得了重大突破。 2016 年 12 月, 版本也带来了一系列增强功能。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 8 性能展望文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

这是一个前途光明的版本,目前正在开发当中,由 Zend 的 Dmitry Stogov 主导。虽然它是基于 PHP 版本基础,但实际版本号尚未定义,所以本文称这个版本为“试验 JIT”分支下。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

关键功能 JIT( :Just-In-Time)编译,是一种将代码转换为另一种字节码(比如运行它的机器 CPU 的本地代码)的技术。 JIT 可以使程序运行更快。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

本文涵盖了几个基准测试的结果,从 PHP 5 的第一个版本到 PHP 的试验性 JIT 分支版本,PHP 5 之前的版本性能本文不作介绍。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

在写这篇文章的时候,我们很难确定 PHP 8 之前是否会有另一个主要版本,比如 PHP 7.2。但是可以假设在 PHP 8 发布时,它已经包括当前试验版 JIT 分支的强大功能。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 性能评估文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

本文只运行纯 CPU 任务脚本的基准测试(不需要I / O操作的任务例如访问文件,网络或数据库连接)。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

使用的基准测试脚本如下所示:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

基准脚本仅使用每个PHP主要版本的最新小版本运行。因此,测试的版本如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

当然,我想确定,我们在相同的基准上运行所有小版本,例如在 到 之间。结果是有说服力的:性能方面的主要增强不是由小版本带来的,而是主要版本号的变化,例如从 PHP 5.4 到 PHP 5.5,或从PHP 5.6 到 PHP 7。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

小版本没有显示任何明显的性能改进。这意味着相同的脚本应该以相同的速度运行,无论您使用 PHP 5.4.0 还是 PHP 5.4.45。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

您可以查看基准进程部分,详细说明主机系统的设置,各个基准的运行方式以及如何解释时序结果。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

纯 CPU 基准测试结果文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

这部分给出了每个 PHP 版本的基准测试结果。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

每个基准列显示 3 个值:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

纯CPU基准测试的结果如下所示:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 5到PHP 7性能全评测(含未发布的JIT版PHP 8对比)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 5到PHP 7性能全评测(含未发布的JIT版PHP 8对比)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

当然,这些都是纯 CPU 的基准测试。它们不涵盖 PHP 性能的所有方面,它们可能不代表真实情况。但是结果足够显著,足以说明几个方面的问题:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

本节介绍了 3 个纯 CPU 基准测试脚本的结果。在运行通常执行的以数据库或文件访问典型场景的 PHP 应用程序时,它不会给出同样的数字,但我认为他们能够代表您对代码的某些部分期望的性能改进。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 每个版本的性能提升文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 5 相比 PHP 4 带来了明显的改进。 Zend Engine 是 PHP 解释器的核心,它已经完全重新设计( Zend Engine 2),为将来的增强功能奠定了基础。本文不多介绍 PHP 4 和 PHP 5 之间的差异,只简要概述的 PHP 之后发生了什么。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

以下部分列出了在后续 PHP 版本中的改进。请注意,这里仅列出影响 PHP 核心的修改。有关更完整的描述,请查看 PHP 5 和 PHP 7 的change log。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 5.1文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 5.2文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 5.3文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 5.4文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 5.5文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 5.6文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 7.0文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

下面大部分列出的改进都与 Zend Engine 相关:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

PHP 8 / 下一代试验性 JIT 分支版文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

性能如何衡量文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

基准化比单纯运行 Unix 时间命令来测量脚本的执行有所区别。 这就是为什么我经历了以下步骤:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

配置系统文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

首先我设置了一个具有以下特性的专用系统:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

虽然系统捆绑了Gnu C编译器版本,但需要升级到更新的版本。 试验性 JIT 分支必须用Gnu C> = 编译。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

编译源代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

在构建完整发行版之前,使用以下选项运行配置脚本:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--prefix=/usr/local/php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--disable-debug文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--disable-phpdbg文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-mysqlnd文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-bcmath文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-bz2=/usr文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-calendar文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-curl文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-exif文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-fpm文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-freetype-dir文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-ftp文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-gd文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-gd-jis-conv文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-gd-native-ttf文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-gettext=/usr文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-gmp文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-iconv文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-intl文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-jpeg-dir文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-mbstring文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-mcrypt文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-openssl文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-pcntl文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-pdo-mysql=mysqlnd文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-png-dir文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-recode=/usr文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-shmop文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-soap文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-sockets文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-sysvmsg文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-sysvsem文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-sysvshm文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-wddx文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-xmlrpc文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-xsl文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-zlib=/usr文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--enable-zip文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

--with-mysqli=mysqlnd文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

注意,在编译旧版时,上面的一些选项需要被禁用或被其他替代,并且并不是所有的扩展都可用或可以被编译。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

运行基准测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

每个基准测试都使用 PHP CLI 专用脚本运行,该脚本遵循以下步骤:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

$__start__ = microtime( true );文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

/***文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

original benchmark code here文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

***/文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

fprintf( STDERR, microtime( true ) - $__start__);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

?>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

文件如下所示:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

engine = On文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

short_open_tag = Off文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

realpath_cache_size = 2M文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

max_execution_time = 86400文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

memory_limit = 1024M文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

error_reporting = 0文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

display_errors = 0文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

display_startup_errors = 0文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

log_errors = 0文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

default_charset = "UTF-8"文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

[opcache]文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

zend_extension=opcache.so文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.enable=1文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.enable_cli=1文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.optimization_level=-1文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.fast_shutdown=1文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.validate_timestamps=1文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.revalidate_freq=60文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.use_cwd=1文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.max_accelerated_files=100000文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.max_wasted_percentage=5文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.memory_consumption=128文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.consistency_checks=0文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.huge_code_pages=1文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

// PHP 8/Next only文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.jit=35文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

opcache.jit_buffer_size=32M文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

分析运行结果文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

使用 Unix time 命令来计时,输出如下所示:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

$ time php bench.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

real: 0m1.96s文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

user: 0m1.912s文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

sys: 0m0.044s文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

第一个值,real :, 是命令开始到终止之间的时间(当你回到 shell 提示符)。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

第二个值,user :,说明在用户模式中花费的时间(在我们的例子中,这是在 php 可执行文件中花费的时间)。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

最后一个值 sys :,说明在操作系统(内核)调用中花费的时间。这个值应该是最小的,但是如果你的代码访问缓慢的设备结果会比较大。重负载的操作系统也可能影响此处报告的值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

在空闲系统上通常,数量(user + sys)应该非常接近 real。这是在上面的例子中的情况:user + sys = ,real 是 。 的差异不属于当前进程:它仅仅意味着操作系统执行任务所花费的额外时间,例如调度。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

同一个脚本在一个负载很重的系统上执行,并行编译 3 个不同的 PHP 版本:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

$ time php bench.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

real: 0m7.812s文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

user: 0m2.02s文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

sys: 0m0.101s文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

在这里我清楚地看到,系统本身的重负载对使用的时间(也许在系统时间)有重大影响。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

这就是为什么我在这个基准中保留一个额外的值,操作系统开销,这是调用的时间和(用户+系统)时间之间的差。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

在纯 CPU 基准测试活动期间,我确保在 99% 以上的时间,这个值严格小于 100 毫秒,即使运行需要几十秒钟完成的脚本。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

特别鸣谢文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

特别鸣谢 Dmitry Stogov 和所有 PHP 核心开发者们。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

本文是和 Dmitry Stogov 合作完成的 , 他帮助审阅了文章内容 , 来保证这个文章的正确性。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

Dmitry Stogov 曾经是 Truck MMCache 的开发者,在 PHP4 时代就可以用作共享内存中缓存 PHP Opcode,从那时候起,Dmitry Stogov 就加入了 Zend,一直到现在。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

Dmitry 是 PHP NG 的主要开发者 , 也就是我们后来知道的 PHP7, 和 Dmitry 一起合作的有 Xinchen Hui(鸟哥),Nikita Popov,正是他们在一起开发了 PHP7 以及后来的版本包括 PHP JIT。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

在 PHP7 之前 , PHP5 时代的 Andi Gumans, Zeev Suraski 以及 Stas Malishev 等也做了很多的工作来提升 PHP5 的性能,限于篇幅,本文就不详细介绍。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

结论文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

本文的目的是给你一个不同版本PHP性能的概述,从 开始,到当前正在开发的最新版本,使用一组已知的基准脚本。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

它还为您提供了由每个连续 PHP 版本解决的性能改进方面的列表。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

本文将随着新的 PHP 版本的公布而更新,并且将来会添加新的基准测试结果。 我也希望添加一些真实世界的 PHP 应用程序,如 WordPress 的基准测试结果。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/13035.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/13035.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定