+ -
当前位置:首页 → 问答吧 → 提升PHP性能之改变Zend引擎分发方式

提升PHP性能之改变Zend引擎分发方式

时间:2009-10-15

来源:互联网

本帖最后由 laruence 于 2009-10-15 17:22 编辑

· 作者:laruence(http://www.laruence.com/)
· 本文地址: http://www.laruence.com/2009/10/15/1131.html
· 转载请注明出处

从PHP5.1开始,PHP提供了用户对Zend VM执行分发方式的选择接口.
之前的文章中, 我也提过这方面的内容, Zend虚拟机在执行的时候, 对于每个Opcode都会分发执行, 按照分发的方式不同可以分为CALL, SWITCH, 和GOTO.
默认是CALL方式, 也就是所有的opcode处理器都定义为函数, 然后虚拟机调用. 这种方式是传统的方式, 也一般被认为是最稳定的方式.
SWITCH方式和GOTO方式则和其命名的意义相同, 分别通过switch和goto来分发.
官方给出的描述是:

CALL - Uses function handlers for opcodes
SWITCH - Uses switch() statement for opcode dispatch
GOTO - Uses goto for opcode dispatch (threaded opcodes architecture)
GOTO is usually (depends on CPU and compiler) faster than SWITCH, which
tends to be slightly faster than CALL.
CALL is default because it doesn’t take very long to compile as opposed
to the other two and in general the speed is quite close to the others.

那么如果使用GOTO方式, 效率上到底能提高多少呢?
今天我就分别使用各种方式来测试一番, 测试脚本bench.php.
第一点被证明的就是, 官方说的GOTO方式编译耗时显著高于其他俩种方式, 我一开始在虚拟机上编译, 每次都Hangup(囧), 最后只好换了个强劲点的物理机, 大约3分钟后, 编译成功..
测试环境:
PHP 5.3.0   LinuxAMD Opteron(tm) Processor 270(2G) X 4  6G Memory编译参数:
./configure --with-zend-vm=CALL/GOTO/SWITCH测试结果如下:
CALL方式:
第一次:[email protected]$ sapi/cli/php bench.phpsimple             0.358simplecall         0.418simpleucall        0.405simpleudcall       0.424mandel             1.011mandel2            1.238ackermann(7)       0.375ary(50000)         0.083ary2(50000)        0.075ary3(2000)         0.561fibo(30)           1.156hash1(50000)       0.114hash2(500)         0.091heapsort(20000)    0.270matrix(20)         0.276nestedloop(12)     0.599sieve(30)          0.350strcat(200000)     0.039------------------------Total              7.844第二次:[email protected]$ sapi/cli/php bench.phpsimple             0.355simplecall         0.413simpleucall        0.402simpleudcall       0.421mandel             1.001mandel2            1.225ackermann(7)       0.371ary(50000)         0.082ary2(50000)        0.073ary3(2000)         0.555fibo(30)           1.154hash1(50000)       0.112hash2(500)         0.089heapsort(20000)    0.267matrix(20)         0.276nestedloop(12)     0.599sieve(30)          0.351strcat(200000)     0.039------------------------Total              7.786SWITCH方式:
第一次:[email protected]$ sapi/cli/php bench.phpsimple             0.393simplecall         0.414simpleucall        0.424simpleudcall       0.445mandel             1.007mandel2            1.254ackermann(7)       0.392ary(50000)         0.084ary2(50000)        0.073ary3(2000)         0.593fibo(30)           1.185hash1(50000)       0.120hash2(500)         0.092heapsort(20000)    0.285matrix(20)         0.295nestedloop(12)     0.678sieve(30)          0.359strcat(200000)     0.042------------------------Total              8.138第二次:[email protected]$ sapi/cli/php bench.phpsimple             0.393simplecall         0.414simpleucall        0.424simpleudcall       0.445mandel             1.007mandel2            1.254ackermann(7)       0.393ary(50000)         0.084ary2(50000)        0.074ary3(2000)         0.593fibo(30)           1.184hash1(50000)       0.120hash2(500)         0.092heapsort(20000)    0.287matrix(20)         0.295nestedloop(12)     0.651sieve(30)          0.359strcat(200000)     0.042------------------------Total              8.114GOTO方式 :
第一次:[email protected]$ sapi/cli/php bench.phpsimple             0.306simplecall         0.373simpleucall        0.369simpleudcall       0.385mandel             0.879mandel2            1.167ackermann(7)       0.355ary(50000)         0.080ary2(50000)        0.072ary3(2000)         0.525fibo(30)           1.045hash1(50000)       0.110hash2(500)         0.088heapsort(20000)    0.246matrix(20)         0.248nestedloop(12)     0.519sieve(30)          0.332strcat(200000)     0.037------------------------Total              7.137第二次:[email protected]$ sapi/cli/php bench.phpsimple             0.306simplecall         0.373simpleucall        0.369simpleudcall       0.385mandel             0.879mandel2            1.132ackermann(7)       0.356ary(50000)         0.081ary2(50000)        0.073ary3(2000)         0.525fibo(30)           1.043hash1(50000)       0.111hash2(500)         0.088heapsort(20000)    0.247matrix(20)         0.247nestedloop(12)     0.519sieve(30)          0.331strcat(200000)     0.037------------------------Total              7.103可见, GOTO方式最快, SWITCH方式最慢. 和官方的描述稍有不符.
GOTO方式比其默认的CALL方式, 性能提升还是比较明显的.
所以, 如果你希望让PHP发挥到机制, 改变Zend VM的分发方式, 也可以做为一个考虑因素.

作者: laruence   发布时间: 2009-10-15

围观老轮屎。

作者: 绿霸花季护航   发布时间: 2009-10-15